0

I want to run a selector using NSTimer which contains some network calls and some other tasks. I want to do that on global queue.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSTimer * myTimer=[[NSTimer alloc]init];
        myTimer = [NSTimer timerWithTimeInterval:10*60 target:self selector:@selector(syncGroupAutomatically) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop] run];
    });

-(void)syncGroupAutomatically
{
    NSLog(@"Some Network calls and some other things\n");
}

If I run this code, it works fine when app is in foreground, but as soon as I press the home button it stops calling the syncGroupAutomatically method.

If anybody having any idea how to run this NSTimer even when app is in the background. Please help me.

Rajan Balana
  • 3,775
  • 25
  • 42
  • 2
    When app goes to background all queues ascociated with the apps are stopped/paused. Only thing you can do to run in background is to opt for one of the many background modes provided by apple. And everytime your app recieves the control back make a webservice call on global queue :) – Sandeep Bhandari Apr 28 '16 at 13:04
  • Thanks @SandeepBhandari – Shivang Garg Apr 29 '16 at 05:01

0 Answers0