In Xcode 11, there is a new Background Mode, "Background Processing". I cannot find any information on what this new Background Mode does.
Are there any resources with that information?
This mode can somehow effect application that is using location updates(Region monitoring and SLC) in background?
iOS restricts the use of background operations to improve user experience and extend battery life. Your app can run in the background for specific use cases, including: playing audio, updating location and fetching the latest content from a server.
An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.
Select your app's target in Xcode and select the Capabilities tab. Under the Capabilities tab, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in Picture” option under the list of available modes.
The background Fetch API allows an app to get updated content when the app isn't running in the foreground. iOS intelligently schedules the background fetch events based on your app usage so when you open your app the content is always up to date.
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:
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].
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.
"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.
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:
[Background session]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With