What is the Xcode "Background Processing" Background Mode?
Asked Answered
C

3

51

In Xcode 11, there is a new Background Mode, "Background Processing". I cannot find any information on what this new Background Mode does.

Picture of the Background Mode features

Are there any resources with that information?

This mode can somehow effect application that is using location updates(Region monitoring and SLC) in background?

Candlepower answered 25/7, 2019 at 8:52 Comment(1)
I have the same question. I activated this feature, but it doesn't seems like there is any effect on the app. My app perform some background task, that's why I activated it.Straka
S
55

There is no documentation yet. But in WWDC2019, they explain what it is and how to use it. Here is the link: Apple WWDC 2019

Say like you wanted to clean up your database in background to delete old records. First, you have to enable background processing in your Background Modes Capabilities. Then in your Info.plist add the background task scheduler identifier: Snapshot from an Info.plist file showing permitted background task scheduler identifiers.

Then in 'ApplicationDidFinishLaunchingWithOptions' method register your identifier with the task.

BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.apple-samplecode.ColorFeed.db_cleaning", using: nil) { task in
    // Downcast the parameter to a processing task as this identifier is used for a processing request
    self.handleDatabaseCleaning(task: task as! BGProcessingTask)
}

Do the work that you wanted to perform in the background and put it into the operation queue. In our case, the cleanup function will looks like:

// Delete feed entries older than one day...
func handleDatabaseCleaning(task: BGProcessingTask) {
    let queue = OperationQueue()
    queue.maxConcurrentOperationCount = 1

    // Do work to setup the task
    let context = PersistentContainer.shared.newBackgroundContext()
    let predicate = NSPredicate(format: "timestamp < %@", NSDate(timeIntervalSinceNow: -24 * 60 * 60))
    let cleanDatabaseOperation = DeleteFeedEntriesOperation(context: context, predicate: predicate)

    task.expirationHandler = {
        // After all operations are canceled, the completion block is called to complete the task
        queue.cancelAllOperations()
    }

    cleanDatabaseOperation.completionBlock {
        // Perform the task
    }

    // Add the task to the queue
    queue.addOperation(cleanDatabaseOperation)
}

Now, when the app goes into the background we have to schedule the background task in BGTaskScheduler.

Note: BGTaskScheduler is a new feature to schedule multiple background tasks that will be performed into the background].

enter image description here

This background task will execute once a week to clean up my database. Check out the properties you can mention to define the task types.

Straka answered 25/9, 2019 at 14:49 Comment(9)
Is there no feature in the API to have a handle on how frequently the BGTasks are called? I.e. the only way to control it from being called too much is to do it ourselves like you did comparing now to once a week?Tartar
What is the difference between BGAppRefreshTaskRequest vs BGProcessingTaskRequest except the allowed execution time and why not always just use the BGProcessingTaskRequest.Acidulant
BGProcessingTaskRequest is for “tasks that can take minutes” while Refresh is for shorter tasks, like a quick data update.Maimonides
Can we trigger this when the app is in background also?Computerize
Same question: can we trigger this when the app is in background and app is fully closed ?Shiau
Can this be simplified for newer devs to swift? Is there a simple hello world version of this using BGProcessingTask? Also it's very confusing to see things like a queue being created in a function and then discarded after the function returns. Is this actually viable testing code?Luminous
'BGTaskScheduler' is only available in iOS 13.0 or newerTriolet
Any idea how to use this for ionic appSellma
Can we use this to run a periodic task in background? I mean an hourly network request?Marenmarena
C
6

"Background Processing" mode is required for running BGTaskScheduler tasks.

BGTaskScheduler:

A class for scheduling tasks run by submitting task requests that launch your app in the background. Configuring the App for Background Tasks Configure the app for background tasks by adding the capabilities for the required background modes, and by adding a whitelist of task identifiers.

Configuring the App for Background Tasks

Configure the app for background tasks by adding the capabilities for the required background modes, and by adding a whitelist of task identifiers. enter image description here

Candlepower answered 21/5, 2020 at 7:50 Comment(0)
W
5

Xcode Background Modes

App states

foreground -> background -> suspended -> terminated

background transfer - execute some task when app is in background mode

To add a capability to work in background mode

App Target -> Signing & Capabilities -> + Capability -> Background Modes

You can find a list of modes like:

  • Audio - recording/playing audio in background mode
  • Location - receive new location updates in background mode
  • Background tasks
    • Background fetch - Background App Refresh Task - 30 seconds to fetch up-to-date data before loading app.
    • Background Processing Task - several minutes at system friendly times (e.g. right after app become background) to complete big task(clear video stuff) or priority task(send message)

[Background session]

Wozniak answered 26/2, 2021 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.