函数不从 swift 中的 NSTimer 选择器调用(在模拟器中工作但不在设备中工作)
Function not calling from NSTimer selector in swift (work in simulator but not in device)
我想检查我的应用程序可以在后台停留多少时间。所以我用这个代码管理它
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)
但它只是第一次调用函数
您是否试过像这样声明您的方法:
func update(sender: NSTimer?) -> Void {
print("func call")
}
并确保你这样称呼它(注意分号:
func applicationDidEnterBackground(application: UIApplication) {
print("entered in background")
let timer = NSTimer.scheduledTimerWithTimeInterval(60 * 5, target: self, selector: "update:", userInfo: nil, repeats: true)
timer.fire()
}
当您的应用程序进入后台时,无法保证计时器会触发。在您的任务被 iOS 停止之前您可能有一些时间,但有时您会得到 none,例如当设备被锁定时。
如果你想 运行 后台的东西,你需要看看 Background Mode Execution
我想检查我的应用程序可以在后台停留多少时间。所以我用这个代码管理它
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)
但它只是第一次调用函数
您是否试过像这样声明您的方法:
func update(sender: NSTimer?) -> Void {
print("func call")
}
并确保你这样称呼它(注意分号:
func applicationDidEnterBackground(application: UIApplication) {
print("entered in background")
let timer = NSTimer.scheduledTimerWithTimeInterval(60 * 5, target: self, selector: "update:", userInfo: nil, repeats: true)
timer.fire()
}
当您的应用程序进入后台时,无法保证计时器会触发。在您的任务被 iOS 停止之前您可能有一些时间,但有时您会得到 none,例如当设备被锁定时。 如果你想 运行 后台的东西,你需要看看 Background Mode Execution