目录
目录README.md

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

关于
60.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号