项目作者: lucas34

项目描述 :
Job Scheduler for IOS with Concurrent run, failure/retry, persistence, repeat, delay and more
高级语言: Swift
项目地址: git://github.com/lucas34/SwiftQueue.git
创建时间: 2017-08-10T11:21:35Z
项目社区:https://github.com/lucas34/SwiftQueue

开源协议:MIT License

下载


SwiftQueue

Schedule tasks with constraints made easy.

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

SwiftQueue is a job scheduler for iOS inspired by popular android libraries like android-priority-jobqueue or android-job. It allows you to run your tasks with run and retry constraints.

Library will rely on Operation and OperationQueue to make sure all tasks will run in order. Don’t forget to check our WIKI.

Features

  • Sequential or Concurrent execution
  • Persistence
  • Cancel all, by id or by tag
  • Start / Stop queue

Job Constraints:

  • Delay
  • Deadline
  • Timeout
  • Internet
  • Charging
  • Single instance in queue
  • Retry: Max count, exponential backoff
  • Periodic: Max run, interval delay
  • Experimental Foreground or Background execution

Requirements

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

Installation

SwiftPackageManager (SPM)

To integrate using Apple’s Swift package manager, add the following as a dependency to your Package.swift:

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

Carthage

SwiftQueue is carthage compatible. Add the following entry in your Cartfile:

  1. github "lucas34/SwiftQueue"

Then run carthage update.

CocoaPods

You can use CocoaPods to install SwiftQueue by adding it to your Podfile:

  1. platform :ios, '8.0'
  2. use_frameworks!
  3. pod 'SwiftQueue'

In your application, simply import the library

  1. import SwiftQueue

Example

This example will simply wrap an api call. Create your custom job by extending Job with onRun, onRetry and onRemove callbacks.

  1. // A job to send a tweet
  2. class SendTweetJob: Job {
  3. // Type to know which Job to return in job creator
  4. static let type = "SendTweetJob"
  5. // Param
  6. private let tweet: [String: Any]
  7. required init(params: [String: Any]) {
  8. // Receive params from JobBuilder.with()
  9. self.tweet = params
  10. }
  11. func onRun(callback: JobResult) {
  12. let api = Api()
  13. api.sendTweet(data: tweet).execute(onSuccess: {
  14. callback.done(.success)
  15. }, onError: { error in
  16. callback.done(.fail(error))
  17. })
  18. }
  19. func onRetry(error: Error) -> RetryConstraint {
  20. // Check if error is non fatal
  21. return error is ApiError ? RetryConstraint.cancel : RetryConstraint.retry(delay: 0) // immediate retry
  22. }
  23. func onRemove(result: JobCompletion) {
  24. // This job will never run anymore
  25. switch result {
  26. case .success:
  27. // Job success
  28. break
  29. case .fail(let error):
  30. // Job fail
  31. break
  32. }
  33. }
  34. }

Create your SwiftQueueManager and keep the reference. If you want to cancel a job it has to be done with the same instance.

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

Schedule your job and specify the constraints.

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

Bind your job type with an actual instance.

  1. class TweetJobCreator: JobCreator {
  2. // Base on type, return the actual job implementation
  3. func create(type: String, params: [String: Any]?) -> Job {
  4. // check for job and params type
  5. if type == SendTweetJob.type {
  6. return SendTweetJob(params: params)
  7. } else {
  8. // Nothing match
  9. // You can use `fatalError` or create an empty job to report this issue.
  10. fatalError("No Job !")
  11. }
  12. }
  13. }

3rd Party Extensions

Contributors

We would love you for the contribution to SwiftQueue, check the LICENSE file for more info.

License

Distributed under the MIT license. See LICENSE for more information.