Task and Task.detached

Updated
Nov 2, 2021 9:43 AM
Created
Nov 2, 2021 9:41 AM
Tags

Differences between the method that runs on the main-actor and not one.

final class Object {

  func run_async() async {

  }

  @MainActor
  func run_not_async_mainActor() {

  }

  func run_not_async_noActor() {

  }

  @MainActor
  func run_mainActor() {

    Task.init {
      await run_async()
      run_not_async_mainActor()
      run_not_async_noActor()
    }

    Task.detached { [self] in
      await run_async()
      await run_not_async_mainActor()
      run_not_async_noActor()
    }

  }

  func run_noActor() {

    Task.init {
      await run_async()
      await run_not_async_mainActor()
      run_not_async_noActor()
    }

    Task.detached { [self] in
      await run_async()
      await run_not_async_mainActor()
      run_not_async_noActor()
    }

  }

}