什么是Xcode“后台处理"?后台模式? [英] What is the Xcode "Background Processing" Background Mode?

查看:146
本文介绍了什么是Xcode“后台处理"?后台模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 11中,有一个新的后台模式"Background Processing".我找不到有关此新背景模式功能的任何信息.

是否有包含该信息的资源?

此模式可以以某种方式影响在后台使用位置更新(区域监视和SLC)的应用程序吗?

解决方案

尚无文档.但是在WWDC2019中,他们解释了它是什么以及如何使用它.链接在这里:

然后在"ApplicationDidFinishLaunchingWithOptions"方法中向任务注册您的标识符.

  BGTaskScheduler.shared.register(forTaskWithIdentifier:"com.example.apple-samplecode.ColorFeed.db_cleaning",使用:nil){任务在//将参数下传到处理任务,因为此标识符用于处理请求self.handleDatabaseCleaning(task:task as!BGProcessingTask)} 

在后台执行您想要执行的工作,并将其放入操作队列中.在我们的例子中,清理功能将如下所示:

 //删除早于一天的提要条目...func handleDatabaseCleaning(task:BGProcessingTask){让队列= OperationQueue()queue.maxConcurrentOperationCount = 1//做工作来设置任务让上下文= PersistentContainer.shared.newBackgroundContext()let谓词= NSPredicate(格式:"timestamp<%@",NSDate(timeIntervalSinceNow:-24 * 60 * 60))让cleanDatabaseOperation = DeleteFeedEntriesOperation(context:上下文,谓词:谓词)task.expirationHandler = {//取消所有操作后,将调用完成块以完成任务queue.cancelAllOperations()}cleanDatabaseOperation.completionBlock {//执行任务}//将任务添加到队列queue.addOperation(cleanDatabaseOperation)} 

现在,当应用程序进入后台时,我们必须在 BGTaskScheduler 中安排后台任务.

注意: BGTaskScheduler 是一项新功能,用于计划将在后台执行的多个后台任务.

此后台任务每周执行一次以清理数据库.检查您可以提及的属性以定义任务类型.

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?

解决方案

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.

这篇关于什么是Xcode“后台处理"?后台模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆