在 Xcode 11 中,有一个新的后台模式,“后台处理”。我找不到有关此新背景模式功能的任何信息。
有这些信息的资源吗?
这种模式会以某种方式影响在后台使用位置更新(区域监控和 SLC)的应用程序吗?
在 Xcode 11 中,有一个新的后台模式,“后台处理”。我找不到有关此新背景模式功能的任何信息。
有这些信息的资源吗?
这种模式会以某种方式影响在后台使用位置更新(区域监控和 SLC)的应用程序吗?
还没有文档。但是在 WWDC2019 中,他们解释了它是什么以及如何使用它。这是链接: Apple WWDC 2019
假设您想在后台清理数据库以删除旧记录。首先,您必须在后台模式功能中启用后台处理。然后在您添加后台任务调度程序标识符:
Info.plist
然后在“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)
}
在后台执行您想要执行的工作并将其放入操作队列中。在我们的例子中,清理函数看起来像:
// 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)
}
现在,当应用程序进入后台时,我们必须在BGTaskScheduler
.
注意:
BGTaskScheduler
是一项新功能,用于安排将在后台执行的多个后台任务]。
此后台任务将每周执行一次以清理我的数据库。查看您可以提及的属性以定义任务类型。
运行 BGTaskScheduler 任务需要“后台处理”模式。
通过提交在后台启动您的应用程序的任务请求来运行调度任务的类。为后台任务配置应用程序 通过添加所需后台模式的功能以及添加任务标识符白名单来为后台任务配置应用程序。
为后台任务配置应用程序
应用状态
foreground -> background -> suspended -> terminated
background transfer
- 当应用程序处于后台模式时执行一些任务
添加在后台模式下工作的功能
App Target -> Signing & Capabilities -> + Capability -> Background Modes
您可以找到以下模式列表: