A dead-simple abstraction over the iOS BackgroundTask API to make background tasks easy to isolate, maintain and schedule. Designed to be as lightweight and flexible as possible while tightly integrating with the system APIs. And It's built with Swift Concurrency in mind.
- Isolates and abstracts background tasks
- Eliminates boilerplate and extra setup steps
- Supports Swift Concurrency / async await
- Low-overhead and full feature set
- Extensive documentation (available with DocC)
This package requires a minimum deployment target of iOS 13.0 and Swift 5.6.
You can install, or integrate, Lurker using Swift Package Manager or manually.
Copy the following URL and then from Xcode choose File
> Add Packages...
.
https://github.com/Sam-Spencer/Lurker.git
Clone or download the repository and copy the contents of the Sources
directory into your project.
Lurker provides stellar documentation to walk you through every step of the way and any questions you may have. But, I've also included a quick reference to get you going here.
Registering and scheduling your tasks can be as short as two lines of code. Just make sure to call Lurker's registerMissions
function before your app finishes launching.
- Register your "missions" (background tasks).
- Schedule them.
- All done! 🍾
func setupLurker() {
do {
try Lurker.shared.registerMissions([ProductMission(), ConfigurationMission()])
Lurker.shared.scheduleAllMissions()
} catch let error {
print("Failed to register and schedule background tasks: \(error)")
}
}
Important: Any errors thrown here are likely programmer errors and should be resolved prior to deployment to production.
Creating a task is pretty easy. Just create an object that conforms to the Mission
protocol and implement the necessary properties and functions.
final class ConfigurationMission: Mission {
// This should match one of your app's predefined task identifier
var identifier: String {
return "com.yourApp.backgroundRefresh.configurationTask"
}
// This can be either "brief" or "extended"
var style: MissionStyle {
return .extended
}
// This is where the magic happens!
func runTask(_ taskInfo: BGTask) async -> Bool {
let longTask = Task { _ -> Bool in
// Perform work here
return true
}
taskInfo.expirationHandler = {
print("Task is expiring")
longTask.cancel()
}
let success = await longTask.value
return success
}
// Return a date here to delay system task execution
func earliestStart() -> Date? {
return nil
}
}
Tip: The
Mission
protocol requiresSendable
conformance. The easiest way to ensure this is by using either astruct
orfinal class
, depending on your needs. Otherwise you may need to do extra work to conform.
Extensive, beautiful documentation is available by importing the included .doccarchive
bundle into Xcode. Just open the archive and Xcode will import it into your documentation browser. Documentation includes articles to get you up and running with Background Tasks and information on how to debug these tasks.
Lurker is available under the MIT license. See LICENSE file for more info.