iOS4 创建后台定时器
我(基本上)需要在 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:
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.
toTarget:
withObject:];
Help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 Grand Central Dispatch (GCD) 来执行此操作。这样,您可以使用块将代码保存在一个位置,并且如果您需要在完成后台处理后更新 UI,请确保再次调用主线程。这是一个基本示例:
Grand Central Dispatch (GCD) 参考:
http://developer.apple.com/ Library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
块参考:
http://developer .apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23//apple_ref/doc/uid/TP40009758
后台任务参考:
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:
You can also do this using Grand Central Dispatch (GCD). This way you can use blocks to keep your code in one place, and be sure you're calling the main thread again if you need to update your UI after you've finished background processing. Here's a basic example:
Grand Central Dispatch (GCD) reference:
http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
Blocks reference:
http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23//apple_ref/doc/uid/TP40009758
Background Task reference:
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:
您可以使用这些调用在新线程 (detachNewThred) 中执行带有某些参数 (withObject) 的对象 (toTarget) 的方法(选择器)。
现在,如果您想执行延迟任务,最好的方法可能是performSelector:withObject:afterDelay:,如果您想在后台运行任务,请调用detachNewThreadSelector:toTarget:withObject:
You can use those call to execute a method (selector) of an object (toTarget) with some parameter (withObject) in a new thread (detachNewThred).
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 thedetachNewThreadSelector: toTarget: withObject:
这些建议的方法是否仅适用于应用程序首先启用后台执行(使用 UIBackgroundMode)的情况?
我想如果一个应用程序不能合法地声称自己是一个 voip/音乐/位置感知应用程序,那么如果它实现了此处描述的内容,那么当时间间隔到期时它不会执行?
Are these suggested methods only applicable if the application is enabled for background execution (using UIBackgroundMode) in the first place?
I presume if an application cannot legitimately claim to be a voip/music/location aware app then if it implements what is described here it will not execute when the time interval expires?