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.
Help would be appreciated!
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?
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:
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: