SwiftQueue 6.0.0

SwiftQueue 6.0.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布2022年6月
SPM支持 SPM

Lucas Nelaupe 维护。



  • 作者
  • Lucas Nelaupe

SwiftQueue

轻松安排有约束的任务。

Awesome platform swift Swift codecov pod Carthage compatible Swift Package Manager compatible Documentation

SwiftQueue 是一个受流行的 Android 库(如 android-priority-jobqueueandroid-job)启发的 iOS 调度器。它允许您按照运行和重试约束来执行任务。

库将依赖于 OperationOperationQueue 以确保所有任务按顺序运行。别忘了查看我们的 维基

功能

  • 顺序或并发执行
  • 持久性
  • 通过 id 或标签取消所有
  • 启动/停止队列

任务约束

  • 延迟
  • 截止日期
  • 超时
  • 网络
  • 充电
  • 队列中单例
  • 重试:最大次数,指数退避
  • 周期性:最大运行次数,间隔延迟
  • 实验性 前台或后台执行

要求

  • iOS 8.0+、watchOS 2.0+、macOS 10.10+、tvOS 9.0+
  • Xcode 11.0

安装

SwiftPackageManager (SPM)

要使用 Apple 的 Swift 包管理器进行整合,请在您的 Package.swift 中添加以下依赖项

.package(url: "https://github.com/lucas34/SwiftQueue.git", .upToNextMajor(from: "4.0.0"))

Carthage

SwiftQueue 兼容 carthage。在您的 Cartfile 中添加以下条目

github "lucas34/SwiftQueue"

然后运行 carthage update

CocoaPods

您可以通过将 SwiftQueue 添加到您的 Podfile 使用 CocoaPods

platform :ios, '8.0'
use_frameworks!
pod 'SwiftQueue'

在您的应用中,只需导入库即可

import SwiftQueue

示例

此示例将简单地包装一个 API 调用。通过扩展 Job 并添加 onRunonRetryonRemove 回调来自定义您的作业。

// A job to send a tweet
class SendTweetJob: Job {
    
    // Type to know which Job to return in job creator
    static let type = "SendTweetJob"
    // Param
    private let tweet: [String: Any]

    required init(params: [String: Any]) {
        // Receive params from JobBuilder.with()
        self.tweet = params
    }

    func onRun(callback: JobResult) {
        let api = Api()
        api.sendTweet(data: tweet).execute(onSuccess: {
            callback.done(.success)
        }, onError: { error in
            callback.done(.fail(error))
        })
    }

    func onRetry(error: Error) -> RetryConstraint {
        // Check if error is non fatal
        return error is ApiError ? RetryConstraint.cancel : RetryConstraint.retry(delay: 0) // immediate retry
    }

    func onRemove(result: JobCompletion) {
        // This job will never run anymore  
        switch result {
            case .success:
                // Job success
            break
            
            case .fail(let error):
                // Job fail
            break
       
        }
    }
}

创建您的 SwiftQueueManager 并保留其引用。如果要取消作业,必须使用相同的实例来完成。

let manager = SwiftQueueManagerBuilder(creator: TweetJobCreator()).build()

安排您的作业并指定约束。

JobBuilder(type: SendTweetJob.type)
        // Requires internet to run
        .internet(atLeast: .cellular)
        // params of my job
        .with(params: ["content": "Hello world"])
        // Add to queue manager
        .schedule(manager: manager)

将您的 job 类型与实际实例绑定。

class TweetJobCreator: JobCreator {

    // Base on type, return the actual job implementation
    func create(type: String, params: [String: Any]?) -> Job {
        // check for job and params type
        if type == SendTweetJob.type  {
            return SendTweetJob(params: params)
        } else {
            // Nothing match
            // You can use `fatalError` or create an empty job to report this issue.
            fatalError("No Job !")
        }
    }
}

第三方扩展

贡献者

非常欢迎您对 SwiftQueue 的贡献,查看 LICENSE 文件以获取更多信息。

许可证

遵照 MIT 许可证分发。更多信息请参阅 LICENSE 文件。