IOS4 Create Background Timer

I (basically) need to create a background timer on iOS 4 that will allow me to execute some code when a certain amount of time has passed. I read that you can accomplish this using some [NSThread detachNewThreadSelector: toTarget: withObject:]; but how does it work in practice? How can I guarantee that the stream will remain in the background. Local notifications will NOT work for me, since I need to execute the code, not notify the user.

Help will be appreciated!

+3
objective-c ios4 background-thread localnotification
Nov 11 2018-10-11
source share
3 answers

You can use this call to execute a method (selector) of an object (toTarget) with some parameter (withObject) in a new thread (detachNewThred).

Now, if you want to perform a deferred task, there may be a better approach - performSelector: withObject: afterDelay: and if you want to run the task when the background is called detachNewThreadSelector: toTarget: withObject:

+4
Nov 11 '10 at 12:52
source share

You can also do this using Grand Central Dispatch (GCD). This way you can use blocks to save the code in one place, and make sure you call the main thread again if you need to update your interface after background processing is complete. Here is a basic example:

 #import <dispatch/dispatch.h> … NSTimeInterval delay_in_seconds = 3.0; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delay_in_seconds * NSEC_PER_SEC); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); UIImageView *imageView = tableViewCell.imageView; // ensure the app stays awake long enough to complete the task when switching apps UIBackgroundTaskIdentifier taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:{}]; dispatch_after(delay, queue, ^{ // perform your background tasks here. It a block, so variables available in the calling method can be referenced here. UIImage *image = [self drawComplicatedImage]; // now dispatch a new block on the main thread, to update our UI dispatch_async(dispatch_get_main_queue(), ^{ imageView.image = image; [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier]; }); }); 

Link to the Central Dispatch Service (GCD): http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

Link to blocks: http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23//apple_ref/doc/uid/TP40009758

Background Information Task: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler :

+20
Nov 14 '10 at 11:04
source share

Are these suggested methods applicable only if the application is enabled for background execution (using UIBackgroundMode)?

I assume that if an application cannot legitimately claim to be a voip / music / location aware application, then if it implements what is described here, will it not run when the time interval expires?

0
Dec 05 '11 at 20:21
source share



All Articles