gpt4 book ai didi

ios - 后台计时器非常慢

转载 作者:行者123 更新时间:2023-11-30 12:06:15 26 4
gpt4 key购买 nike

我正在使用计时器,并尝试使其在后台运行。在模拟器上这工作正常,但在我的设备(iOS 11)上它非常慢:1 秒变成了 5 或 6 秒......

This is my code for run application in background:

backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(
expirationHandler:
{UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)}
)

Info.plist: Application does not run in background : NO

我怎样才能让它发挥作用?

编辑:

这是我的计时器代码:

timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, 
selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)

编辑2:

func updateTimer () {
var j = 0
for _ in rows {
if (rows[j]["Playing"] as! Bool == true ) {
rows[j]["time"] = (rows[j]["time"] as! Double + 0.01) as AnyObject
// print(rows[j]["time"]) - PRINT OUTPUT HERE
rows[j]["lastTime"] = (rows[j]["lastTime"] as! Double + 0.01) as AnyObject
}
if (rows[j]["lastTime"] as! Double > 60.0) {
min[j] += 1
rows[j]["lastTime"] = 0.00 as AnyObject
}
j += 1
}
}

最佳答案

不要只打印可能从不同线程执行的输出,而是从主线程打印它。这就是您可能会看到延迟的原因:

DispatchQueue.main.async {
print(rows[j]["time"])
}

关于ios - 后台计时器非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com