WorkManager is a task scheduler, it allows apps to schedule periodic tasks to perform in certain intervals of duration, while persisting the tasks throughout app launches, and termination cycles.
WorkManager uses UserDeafult to store the last run time and the interval every time the app comes to the foreground your app passes control to WorkManager to let it evaluate and run if the task needed to be run again.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into swift.
To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter below repository URL
https://github.com/gojek/WorkManager.git
Once you have your Swift package set up, adding WorkManager as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.package(url: "https://github.com/gojek/WorkManager.git", .upToNextMajor(from: "0.10.0"))
]CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate WorkManager into your Xcode project using CocoaPods, specify the following pod in your Podfile:
pod 'WorkManager'WorkManager needs you to register a closure with a unique task id, preferably during the app launch sequence, suggested place would be before returning from application(_:, didFinishLaunchingWithOptions:) -> Bool.
import WorkManager
// Scheduling to perform in every 2 days
WorkManager.shared.enqueueUniquePeriodicWork(id: "com.unique.task.id", interval: 2 * 24 * 60 * 60) {
cleanUpDisk()
}In case you want to cancel a scheduled task on the fly you could pass the unique id of the task to WorkManager and call cancel
WorkManager.shared.cancelQueuedPeriodicWork(withId: "com.unique.task.id")WorkManager As of now only performs the tasks in the foreground, it will evaluate every launch time if the time interval has passed against the last run time, which is a response to which it will perform your registered closure.
Refer here for the documentation
As the creators, and maintainers of this project, we're glad to invite contributors to help us stay up to date. Please take a moment to review the contributing document in order to make the contribution process easy and effective for everyone involved.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
WorkManager is available under the MIT license. See the LICENSE file for more info.