Execute async code in the main or background threads. Easily switch between them.
Usage
import Worker
// Execute code in the main thread.
Worker.doMainThreadWork {
print("Main thread work...")
}
// Execute code in the main thread after one second.
Worker.doMainThreadWork(after: 1) {
print("Main thread work after 1 second...")
}
// Execute code in a background thread.
Worker.doBackgroundWork {
print("Background thread work...")
}
// Execute code in a background thread after one second.
Worker.doBackgroundWork(after: 1) {
print("Background thread work after 1 second...")
}
// Execute code in a background thread and update the UI afterwards.
Worker.doBackgroundWork {
print("Something expensive...")
Worker.doMainThreadWork {
print("Update the UI.")
}
}
Worker’s background threads use QoS .userInitiated to execute code asynchronously with a higher priority. From Apple:
User-initiated tasks are second only to user-interactive tasks in their priority on the system. Assign this class to tasks that provide immediate results for something the user is doing, or that would prevent the user from using your app. For example, you might use this quality-of-service class to load the content of an email that you want to display to the user.
Worker
Execute async code in the main or background threads. Easily switch between them.
Usage
Worker’s background threads use QoS
.userInitiated
to execute code asynchronously with a higher priority.From Apple:
Integration
Xcode
Use Xcode’s built-in support for SPM.
Package.swift
In your
Package.swift
, addWorker
as a dependency:Associate the dependency with your target:
Run:
swift build