Wrappers for Swift Concurrency asynchronous actions on controls that enable user interaction specific to each platform and context provided by SwiftUI.
It's based on the following post: https://zenn.dev/treastrain/articles/3effccd39f4056
- ✅ Designed for Swift Concurrency and SwiftUI.
- ✅ Compatible with all Apple platforms (iPhone, iPod touch, iPad, Mac, Apple TV, Apple Watch, Apple Vision Pro).
- ✅ Automatically cancels the task and performs a new action on new user interaction.
- ✅ Automatically cancels the task after the view disappears before the action completes.
- ✅ Supports Sendable and inherits the Actor context.
- ✅ No dependencies.
import AsyncSwiftUI
struct ContentView: View {
@State private var isRunning = false
var body: some View {
AsyncButton(isRunning ? "Running..." : "Run") {
isRunning = true
defer { isRunning = false }
do {
print("START")
try await Task.sleep(for: .seconds(2))
} catch {
print("ERROR:", error)
}
print("FINISH")
}
.disabled(isRunning)
}
}
To use this library in a Swift Package Manager project, add the following line to the dependencies in your Package.swift
file:
.package(url: "https://github.com/treastrain/AsyncSwiftUI", from: "0.1.0"),
Include "AsyncSwiftUI"
as a dependency for your executable target:
.target(name: "<target>", dependencies: [
.product(name: "AsyncSwiftUI", package: "AsyncSwiftUI"),
]),
Finally, add import AsyncSwiftUI
to your source code (or replace the existing import SwiftUI
with it).
SwiftUI | AsyncSwiftUI | Available on |
---|---|---|
Button |
AsyncButton |
iOS 15.0+, iPadOS 15.0+, macOS 12.0+, Mac Catalyst 15.0+, tvOS 15.0+, watchOS 8.0+, visionOS 1.0+ |
EditButton |
(No need) | ― |
PasteButton |
🚧 | ― |
RenameButton |
🚧 | ― |
Link |
(No need) | ― |
ShareLink |
(No need) | ― |
TextFieldLink |
AsyncTextFieldLink |
watchOS 9.0+ |
HelpLink |
🚧 | ― |
Slider |
🚧 | ― |
Stepper |
🚧 | ― |
Toggle |
🚧 | ― |
Picker |
(No need) | ― |
DatePicker |
(No need) | ― |
MultiDatePicker |
(No need) | ― |
ColorPicker |
(No need) | ― |
Gauge |
(No need) | ― |
ProgressView |
(No need) | ― |
ContentUnavailableView |
(No need) | ― |
SwiftUI | AsyncSwiftUI | Available on |
---|---|---|
Menu |
🚧 | ― |
MenuButton |
(No need) | ― |
PullDownButton |
(No need) | ― |
CommandMenu |
(No need) | ― |
CommandGroup |
(No need) | ― |
The documentation comments included in this library, with some exceptions, are from the SwiftUI entry in the Apple Developer Documentation.