Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created October 12, 2022 21:21
Show Gist options
  • Save alexito4/4a9a7c6e3dc447a4f762cf1cf7346b5e to your computer and use it in GitHub Desktop.
Save alexito4/4a9a7c6e3dc447a4f762cf1cf7346b5e to your computer and use it in GitHub Desktop.
SwiftUI View.task backwards compatibility
import Foundation
import SwiftUI
public extension View {
@available(iOS, obsoleted: 15.0, message: "SwiftUI.View.task is available on iOS 15.")
@_disfavoredOverload
@inlinable func task(
priority: _Concurrency.TaskPriority = .userInitiated,
@_inheritActorContext _ action: @escaping @Sendable () async -> Swift.Void
) -> some SwiftUI.View {
modifier(MyTaskModifier(priority: priority, action: action))
}
}
public struct MyTaskModifier: ViewModifier {
private var priority: TaskPriority
private var action: @Sendable () async -> Void
public init(
priority: TaskPriority,
action: @escaping @Sendable () async -> Void
) {
self.priority = priority
self.action = action
self.task = nil
}
@State private var task: Task<Void, Never>?
public func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
content
.task(priority: priority, action)
} else {
content
.onAppear {
self.task = Task {
await action()
}
}
.onDisappear {
self.task?.cancel()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment