gpt4 book ai didi

ios - 函数未在 swift 中从 NSTimer 选择器调用(在模拟器中工作,但不在设备中工作)

转载 作者:行者123 更新时间:2023-11-29 01:34:40 25 4
gpt4 key购买 nike

我想检查我的应用程序可以在后台停留多少时间。所以我用这段代码管理了它

func applicationDidEnterBackground(application: UIApplication) {
print("entered in background")
let timer = NSTimer.scheduledTimerWithTimeInterval(60 * 5, target: self, selector: "update", userInfo: nil, repeats: true)
timer.fire()
}

我的更新函数

func update(){ 
print("func call")
}

所有这些代码都在模拟器中工作,但每当我在设备中运行我的项目时,它都没有调用这个函数。
当应用程序在前台检查计时器是否正常工作时,此功能也可以正常工作。
我正在使用 swift 2.0。
可能应用所有解决方案但没有成功。

编辑:
也应用此代码

NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector(update()), userInfo: nil, repeats: true)

但它只在第一次调用函数

最佳答案

当您的应用程序进入后台时,无法保证计时器会触发。在您的任务被 iOS 停止之前您可能有一些时间,但有时您什么也得不到,例如当设备被锁定时。如果你想在后台运行,你需要看看 Background Mode Execution

关于ios - 函数未在 swift 中从 NSTimer 选择器调用(在模拟器中工作,但不在设备中工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101120/

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