问题描述
我(基本上)需要在 iOS 4 上创建一个后台计时器,它允许我在经过特定时间后执行一些代码.我读过你可以使用一些 [NSThread detachNewThreadSelector:达到目标:withObject:];
但在实践中它是如何工作的呢?如何确保线程也保留在后台.本地通知不对我有用,因为我需要执行代码,而不是通知用户.
I (Basically) need to create a background timer on iOS 4 that will allow me to execute some code when a specific amount of time has passed. I have read that you can accomplish this using some [NSThread detachNewThreadSelector: toTarget: withObject:];
but how does that work in practice? How can I ensure that the thread remains in the background also. Local notifications will NOT work for me, as I need to execute code, not notify the user.
我们将不胜感激!
推荐答案
您可以使用这些调用在新线程 (detachNewThred) 中执行带有某些参数 (withObject) 的对象 (toTarget) 的方法 (选择器).
You can use those call to execute a method (selector) of an object (toTarget) with some parameter (withObject) in a new thread (detachNewThred).
现在,如果您想执行延迟任务,最好的方法可能是 performSelector: withObject: afterDelay:
,如果您想在后台运行任务,请调用 detachNewThreadSelector: toTarget: withObject:
Now if you want to execute a delayed task may be the best approach is performSelector: withObject: afterDelay:
and the if you want to run the task on background call the detachNewThreadSelector: toTarget: withObject:
这篇关于iOS4 创建后台定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!