Git Product home page Git Product logo

lurker's Introduction


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.

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.

lurker's People

Contributors

sam-spencer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.