English 中文(简体)
- 第13条——利用BGTaskStort公司在任何时候工作
原标题:iOS 13 - Using BGTaskScheduler doesn t work all the time

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):

“在座的影像描述”/</a

然后,我根据《国际契约》(Identifier is fake Just for StackOverflow question)编写了信息清单:

“entergraph

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)
            }
        }
    }

现在,当用didEnterBackground方法时,我提交了背景材料。 任务:

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.

我执行背景塔克错了吗? 我在互联网上搜索了所有的人,但并没有遇到任何这个问题的人。

最佳回答

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.

问题回答

这也是我的情况,我发现,在装置环境中,如果你去了总和; 背景申请被定在“WLAN”上,如果把它转到“WLAN”上,如果我的任务开始执行,则不像我要求的那样经常执行,而是在“STILL”工作(它可以帮助某人)。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签