A dead-simple abstraction over the iOS BackgroundTask API to make background tasks easy to isolate, maintain and schedule

Overview


Lurker

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.


Features

  • 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)

Requirements

This package requires a minimum deployment target of iOS 13.0 and Swift 5.6.

Installation

You can install, or integrate, Lurker using Swift Package Manager or manually.

Swift Package Manager

Copy the following URL and then from Xcode choose File > Add Packages....

https://github.com/Sam-Spencer/Lurker.git

Manually

Clone or download the repository and copy the contents of the Sources directory into your project.

Getting Started

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 & Scheduling Tasks

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.

  1. Register your "missions" (background tasks).
  2. Schedule them.
  3. 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

Creating a task is pretty easy. Just create an object that conforms to the Mission protocol and implement the necessary properties and functions.

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 } }">
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 requires Sendable conformance. The easiest way to ensure this is by using either a struct or final class, depending on your needs. Otherwise you may need to do extra work to conform.

Documentation

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.

Documentation Screenshot

License

Lurker is available under the MIT license. See LICENSE file for more info.

You might also like...
Material para a apresentação da palestra "Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS" no TDC Future 2021

--- title: Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS author: Cícero Camargo date: Nov 30th

This app is a sample app that recognizes specific voice commands such as
This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "make it black" and change the background color of the view in the frame.

VoiceOperationSample This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "mak

Reactive WebSockets - A lightweight abstraction layer over Starscream to make it reactive.

RxWebSocket Reactive extensions for websockets. A lightweight abstraction layer over Starscream to make it reactive. Installation RxWebSocket is avail

Simple Background Fetch Tasks

Simple Background Fetch Tasks

🛶Shallows is a generic abstraction layer over lightweight data storage and persistence.

Shallows Shallows is a generic abstraction layer over lightweight data storage and persistence. It provides a StorageKey, Value type, instances of w

CITreeView created to implement and maintain that wanted TreeView structures for IOS platforms easy way
CITreeView created to implement and maintain that wanted TreeView structures for IOS platforms easy way

CITreeView CITreeView created to implement and maintain that wanted TreeView structures for IOS platforms easy way. CITreeView provides endless treevi

Aardvark is a library that makes it dead simple to create actionable bug reports.
Aardvark is a library that makes it dead simple to create actionable bug reports.

Aardvark Aardvark makes it dead simple to create actionable bug reports. Aardvark is made up of a collection of frameworks that provide different bug

Dead simple calendar implementation
Dead simple calendar implementation

Package provides a CalendarView which can be used to display simple calendar in your App.

Dead-simple queue-oriented client for Spotify
Dead-simple queue-oriented client for Spotify

Spotiqueue A terribly simple macOS app for keyboard-based, queue-oriented Spotify use. Many years ago i built a version which relied on a now-deprecat

DaisyChain is a micro framework which makes UIView animations chaining dead simple.

DaisyChain DaisyChain is a micro framework which makes UIView animations chaining dead simple. It uses the exact same interface you are familiars with

A dead simple programming language.

checked Checked is a dead simple programming language. Variable declarations with type inference func main() { let constantVariable = 5 var va

SharePlay-TextEditor - An iOS & iPadOS application, by which people can write/edit texts and change font-styles/background color over FaceTime call
APIProvider - API Provider Package for easier API management inspired by abstraction

APIProvider Using APIProvider you can easily communicate with all API endpoints

Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)

Schedule(简体中文) Schedule is a timing tasks scheduler written in Swift. It allows you run timing tasks with elegant and intuitive syntax. Features Elega

Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)

Schedule(简体中文) Schedule is a timing tasks scheduler written in Swift. It allows you run timing tasks with elegant and intuitive syntax. Features Elega

KHabit an open source, pure and minimalistic app which helps you maintain productive habits, and nothing more.

an open source, pure and minimalistic app which helps you maintain productive habits, and nothing more. The app is completely open source, it does not contain in-app or ads, and does not track the user in any way.

an open source, pure and minimalistic app which helps you maintain productive habits, and nothing more.

KHabit an open source, pure and minimalistic app which helps you maintain productive habits, and nothing more. The app is completely open source, it d

CodeBucket is the best way to browse and maintain your Bitbucket repositories on any iPhone, iPod Touch, and iPad device!
CodeBucket is the best way to browse and maintain your Bitbucket repositories on any iPhone, iPod Touch, and iPad device!

CodeBucket Description CodeBucket is the best way to browse and maintain your Bitbucket repositories on any iPhone, iPod Touch, and iPad device! Keep

An iOS application that helps the user to track his/her investments and maintain portfolios.
An iOS application that helps the user to track his/her investments and maintain portfolios.

CryptoX Technology has changed the way people work, communicate, shop, pay and collaborate. One such transformation was the origin of cryptocurrency.

Releases(1.0.0)
Owner
Sam Spencer
digital media meddler & app developer. fascinated in the intersection of art & science.
Sam Spencer
Bolts is a collection of low-level libraries designed to make developing mobile apps easier.

Bolts in Swift Bolts is a collection of low-level libraries designed to make developing mobile apps easier. Bolts was designed by Parse and Facebook f

null 1.3k Nov 11, 2022
Easy Swift Futures & Promises.

❗️ Archived now ❗️ Since Apple released Combine framework, I decide to archive this repo. You still can use this repo as an example of Future/Promise

Dmytro Mishchenko 40 Sep 23, 2022
A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

null 759 Dec 2, 2022
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

David Ask 60 Aug 11, 2022
A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety

A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety. Type Safe No more userInfo dictionary and Downcasting,

null 632 Dec 7, 2022
Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Thomas Visser 1.9k Dec 20, 2022
The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Promis The easiest Future and Promises framework in Swift. No magic. No boilerplate. Overview While starting from the Objective-C implementation of Ju

Alberto De Bortoli 111 Dec 27, 2022
Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.

Promises Promises is a modern framework that provides a synchronization construct for Objective-C and Swift to facilitate writing asynchronous code. I

Google 3.7k Dec 24, 2022
Futures and Promises library

#PureFutures A simple Futures and Promises library. ##Installation ###Carthage Add the following in your Cartfile: github "wiruzx/PureFutures" And ru

Victor Shamanov 17 Apr 5, 2019
A publish/subscribe EventBus optimized for iOS

SwiftEventBus Allows publish-subscribe-style communication between components without requiring the components to explicitly be aware of each other Fe

César Ferreira 1k Dec 15, 2022