Swiftpack.co - Package - backslash-f/worker

swift-version swift-package-manager platform build-status license

Worker

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.

Integration

Xcode

Use Xcode's built-in support for SPM.

Package.swift

In your Package.swift, add Worker as a dependency:

dependencies: [
  .package(url: "https://github.com/backslash-f/worker", from: "1.0.0")
],

Associate the dependency with your target:

targets: [
  .target(name: "App", dependencies: ["Worker"])
]

Run: swift build

Github

link
Stars: 1

Dependencies

Used By

Total: 0

Releases

v1.1.1 - 2020-10-12 09:16:12

Increase Swift tools version (5.3)

v1.1.0 - 2020-08-05 11:04:39

It's now possible to delay execution on background or main threads.

For example:

import Worker

// 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 after one second.
Worker.doBackgroundWork(after: 1) {
    print("Background thread work after 1 second...")
}

First Release - 2020-02-22 17:30:55