Xcode 11 - 什么是新的“后台处理”后台模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57197958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 11 - What is the new "Background Processing" Background Mode?
提问by phnmnn
In XCode 11, there is a new Background Mode, "Background Processing". I cannot find any information on what this new Background Mode does.
在 XCode 11 中,有一个新的后台模式,“后台处理”。我找不到有关此新背景模式功能的任何信息。
Are there any resources with that information? This mode can somehow effect application that is using location updates(Region monitoring and SLC) in background?
有这些信息的资源吗?这种模式可以以某种方式影响在后台使用位置更新(区域监控和 SLC)的应用程序?
回答by Saleh Sultan
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
还没有文档。但是在 WWDC2019 中,他们解释了它是什么以及如何使用它。这是链接: Apple WWDC 2019
Say like you wanted to clean up your database in background to delete old records. First, you have to enable background processingin your Background ModesCapabilities. Then in your Info.plist
add the background task scheduler identifier:
假设您想在后台清理数据库以删除旧记录。首先,您必须在后台模式功能中启用后台处理。然后在您添加后台任务调度程序标识符:
Info.plist
Then in 'ApplicationDidFinishLaunchingWithOptions' method register your identifier with the task.
然后在“ApplicationDidFinishLaunchingWithOptions”方法中向任务注册您的标识符。
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
.
现在,当应用程序进入后台时,我们必须在BGTaskScheduler
.
Note:
BGTaskScheduler
is a new feature to schedule multiple background tasks that will be performed into the background].
注意:
BGTaskScheduler
是一项新功能,用于安排将在后台执行的多个后台任务]。
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.
此后台任务将每周执行一次以清理我的数据库。查看您可以提及的属性以定义任务类型。
回答by phnmnn
"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.