Created
January 7, 2026 02:29
-
-
Save swhitty/c387b99bd1428c7ea9e7feeb50616c4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public extension Task where Failure == Never { | |
| /// Create and immediately start running a new detached task on the @MainActor. | |
| /// | |
| /// If the current actor is @MainActor, the spawned task immediately takes over | |
| /// execution in a synchronous manner, before returning to the calling method at the | |
| /// first suspension point. | |
| /// | |
| /// If the current actor is not @MainActor then behaviour is the similar to using `Task { }` | |
| /// | |
| @available(iOS, deprecated: 26.0, message: "use Task.immediate") | |
| @MainActor | |
| @discardableResult | |
| static func immediateMainActor( | |
| priority: TaskPriority? = nil, | |
| operation: sending @escaping @MainActor () async -> Success | |
| ) -> Task<Success, Never> { | |
| if #available(iOS 26, *) { | |
| Task.immediate(priority: priority, operation: operation) | |
| } else { | |
| Task.startOnMainActor(priority: priority) { await operation() } | |
| } | |
| } | |
| } | |
| public extension Task where Failure == any Error { | |
| /// Create and immediately start running a new detached task on the @MainActor. | |
| /// | |
| /// If the current actor is @MainActor, the spawned task immediately takes over | |
| /// execution in a synchronous manner, before returning to the calling method at the | |
| /// first suspension point. | |
| /// | |
| /// If the current actor is not @MainActor then behaviour is the similar to using `Task { }` | |
| /// | |
| @available(iOS, deprecated: 26.0, message: "use Task.immediate") | |
| @MainActor | |
| @discardableResult | |
| static func immediateMainActor( | |
| priority: TaskPriority? = nil, | |
| operation: sending @escaping @MainActor () async throws -> Success | |
| ) -> Task<Success, any Error> { | |
| if #available(iOS 26, *) { | |
| Task.immediate(priority: priority, operation: operation) | |
| } else { | |
| Task.startOnMainActor(priority: priority) { try await operation() } | |
| } | |
| } | |
| } | |
| // Task.startOnMainActor shipped in iOS 17/18 as private api behind @_spi(MainActorUtilities). | |
| // While the SDK swiftinterface strips this method, we can use the mangled name to link | |
| // to the symbol at compile time | |
| extension Task where Failure == Never { | |
| @_silgen_name("$sScTss5NeverORs_rlE16startOnMainActor8priority_ScTyxABGScPSg_xyYaYbScMYccntFZ") | |
| @MainActor | |
| @discardableResult | |
| public static func startOnMainActor( | |
| priority: TaskPriority? = nil, | |
| @_inheritActorContext @_implicitSelfCapture _ work: consuming @Sendable @escaping @MainActor() async -> Success | |
| ) -> Task<Success, Never> | |
| } | |
| extension Task where Failure == any Error { | |
| @_silgen_name("$sScTss5Error_pRs_rlE16startOnMainActor8priority_ScTyxsAA_pGScPSg_xyYaYbKScMYccntFZ") | |
| @MainActor | |
| @discardableResult | |
| public static func startOnMainActor( | |
| priority: TaskPriority? = nil, | |
| @_inheritActorContext @_implicitSelfCapture _ work: consuming @Sendable @escaping @MainActor() async throws -> Success | |
| ) -> Task<Success, any Error> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment