Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <a href="https://stackoverflow.com/questions/5187741/nstimer-on-background-is-working/17445679">can</a> have a timer fire while in background execution mode. There are a couple of tricks:</p> <ul> <li>You need to opt into background execution with <code>beginBackgroundTaskWithExpirationHandler</code>. </li> <li>If you create the NSTimer on a background thread, you need to add it to the mainRunLoop manually.</li> </ul> <hr> <pre><code>- (void)viewDidLoad { // Avoid a retain cycle __weak ViewController * weakSelf = self; // Declare the start of a background task // If you do not do this then the mainRunLoop will stop // firing when the application enters the background self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:self.backgroundIdentifier]; }]; // Make sure you end the background task when you no longer need background execution: // [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Since we are not on the main run loop this will NOT work: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerDidFire:) userInfo:nil repeats:YES]; // This is because the |scheduledTimerWithTimeInterval| uses // [NSRunLoop currentRunLoop] which will return a new background run loop // which will not be currently running. // Instead do this: NSTimer * timer = [NSTimer timerWithTimeInterval:0.5 target:weakSelf selector:@selector(timerDidFire:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; // or use |NSRunLoopCommonModes| if you want the timer to fire while scrolling }); } - (void) timerDidFire:(NSTimer *)timer { // This method might be called when the application is in the background. // Ensure you do not do anything that will trigger the GPU (e.g. animations) // See: http://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47 NSLog(@"Timer did fire"); } </code></pre> <p><strong>Notes</strong></p> <ul> <li>Apps only get ~ 10 mins of background execution - after this the timer will stop firing.</li> <li>As of iOS 7 when the device is <em>locked</em> it will suspend the foreground app almost instantly. The timer will not fire after an iOS 7 app is locked.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload