Git Product home page Git Product logo

ios-workmanager's Introduction

iOS WorkManager

iOS WorkManager is a wrapper around BackgroundTasks

Currently only proccesing tasks are supported

It is useful to run periodic tasks, such as fetching remote data on a regular basis.

Installation

Setup

Before you can schedule a background task, you must register each task with a unique identifier in your project's Info.plist:

<key>BGTaskSchedulerPermittedIdentifiers</key>
	<array>
		<string>be.vrt.ios-workmanager.oneoffbackgroundtask</string>
		<string>be.vrt.ios-workmanager.recurringbackgroundtask</string>
	</array>

Also add required UIBackgroundModes:

<key>UIBackgroundModes</key>
	<array>
		<string>processing</string>
	</array>

Registering a task

Register a task inside your AppDelegate didFinishLaunchingWithOptions and provide a callback for when the task is executed:

WorkManager.shared.registerTask(withIdentifier: "be.vrt.ios-workmanager.oneoffbackgroundtask") { task in
    self.handleOneOffTrigger(task: task as! BGProcessingTask)
}

Schedule a task

Two kinds of background tasks can be registered :

  • One off task : runs only once
  • Periodic tasks : runs indefinitely on a regular basis

To schedule a one off task:

WorkManager.shared.scheduleOneOffTask(withIdentifier: "be.vrt.ios-workmanager.oneoffbackgroundtask", name: "update_something")

To schedule a periodic task:

WorkManager.shared.schedulePeriodicTask(withIdentifier: "be.vrt.ios-workmanager.recurringbackgroundtask", name: "update_something", frequency: 900)

Properties

Initial Delay

Indicates how along a task should wait before its first run.

WorkManager.shared.schedulePeriodicTask(withIdentifier: "be.vrt.ios-workmanager.recurringbackgroundtask", name: "update_something", frequency: 900, initialDelay: 900)

Existing Work Policy

Indicates the desired behaviour when the same task is scheduled more than once.
The default is KEEP

WorkManager.shared.scheduleOneOffTask(withIdentifier: "be.vrt.ios-workmanager.oneoffbackgroundtask", name: "update_something", existingWorkPolicy: .replace)

Constraints

WorkManager.shared.schedulePeriodicTask(withIdentifier: "be.vrt.ios-workmanager.recurringbackgroundtask", name: "update_something", frequency: 900, constraints: [.requiresExternalPower, .requiresNetworkConnectivity])

BackoffPolicy

Indicates the waiting strategy upon task failure.
You can also specify the delay.

WorkManager.shared.schedulePeriodicTask(withIdentifier: "be.vrt.ios-workmanager.recurringbackgroundtask", name: "update_something", frequency: 900,backoffPolicy: .exponential, backoffPolicyDelay: 500)

Cancel a task

A task can be cancelled in different ways :

With identifier

WorkManager.shared.cancelTask(withIdentifier identifier: "")

With tag

WorkManager.shared.cancelTask(withTag tag: "")

All

WorkManager.shared.cancelAllTasks()

Example

See example project

ios-workmanager's People

Contributors

olivierscalais avatar

Watchers

James Cloos 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.