Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

ios - What is the Xcode "Background Processing" Background Mode?

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?

question from:https://stackoverflow.com/questions/57197958/what-is-the-xcode-background-processing-background-mode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...