iOS 13-并非始终使用BGTaskScheduler [英] iOS 13 - Using BGTaskScheduler doesn't work all the time

查看:1105
本文介绍了iOS 13-并非始终使用BGTaskScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: 我想在应用程序进入后台5秒后运行一个简单的功能.

我必须实现 BGTaskScheduler ,以支持iOS 13. BackgroundTask的旧实现适用于我的旧iOS版本.

我根据要求添加了背景模式(由于我们在此功能中执行了一个小的BLE操作,因此选中了BLE配件):

然后,我根据文档(标识符为假仅用于StackOverflow问题)准备了Info.plist:

didFinishLaunchingWithOptions 结束之前,我注册了我的BackgroundTask:

if #available(iOS 13.0, *) {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.MyID", using: .global()) { (task) in
            print("My backgroundTask is executed NOW!")
            task.expirationHandler = {
                task.setTaskCompleted(success: true)
            }
        }
    }

现在,当应用程序运行 didEnterBackground 方法时,我将提交BackgroundTaskRequest:

if #available(iOS 13.0, *) {
            do {
                let request = BGAppRefreshTaskRequest(identifier: "com.example.MyID")
                request.earliestBeginDate = Calendar.current.date(byAdding: .second, value: 5, to: Date())
                try BGTaskScheduler.shared.submit(request)

                print("Submitted task request")
            } catch {
                print("Failed to submit BGTask")
            }
        } 

这里的问题是它非常不一致. Apple保证不会在给定日期之前执行任务,但不能保证将在确切的时间执行任务(我很好,但有一点延迟). 但是,当我运行该应用程序时,无论我是否提供了任务请求延迟(使用earlyestBeginDate),它都无法100%地运行,因此,下次我提交该应用程序时,它首先要尝试7秒(而不是5秒)任务耗时26秒,第三次从未到达关闭状态.

我是否以错误的方式实现BackgroundTask?我在互联网上搜索了一些答案,但没有遇到任何遇到此问题的人.

解决方案

正如badhanganesh所说,似乎只有在系统决定执行任务时才会执行任务. 苹果表示,在 WWWDC 2019,会议#707 中.

THE PROBLEM: I want to run a simple function, 5 seconds after app goes into background.

I had to implement BGTaskScheduler, to support iOS 13. The old implementation for BackgroundTask works for me on older iOS versions.

I added background modes as requested (BLE accessories is ticked because we perform a small BLE operation in this function):

Then, I prepared the Info.plist according to the docs (Identifier is fake just for StackOverflow question):

Before didFinishLaunchingWithOptions is ended, I register my BackgroundTask:

if #available(iOS 13.0, *) {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.MyID", using: .global()) { (task) in
            print("My backgroundTask is executed NOW!")
            task.expirationHandler = {
                task.setTaskCompleted(success: true)
            }
        }
    }

Now, when the app run the didEnterBackground method, I submit a BackgroundTaskRequest:

if #available(iOS 13.0, *) {
            do {
                let request = BGAppRefreshTaskRequest(identifier: "com.example.MyID")
                request.earliestBeginDate = Calendar.current.date(byAdding: .second, value: 5, to: Date())
                try BGTaskScheduler.shared.submit(request)

                print("Submitted task request")
            } catch {
                print("Failed to submit BGTask")
            }
        } 

The problem here is that it is VERY inconsistent. Apple guarantee that the task will not be executed before the given date, but does not guarantee that it will be executed on the exact time (I'm fine with a small delay). However, when I ran the app, it did not work 100% of the times, regardless if I provided the task request a delay (using earliestBeginDate) so it used to go first try 7 seconds (instead of 5), next time I submitted the task it took 26 seconds, third time never arrived the closure.

Am I implementing the BackgroundTask the wrong way? I've searched all over the internet some answer but did not encounter anyone having this problem.

解决方案

As badhanganesh said, It seems like the task will be executed only when the system decides to do so. Apple said that during WWWDC 2019, session #707.

这篇关于iOS 13-并非始终使用BGTaskScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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