NSTimer inside global queue is not called when the app is in the background

我与影子孤独终老i 提交于 2019-12-12 03:28:13

问题


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.

来源:https://stackoverflow.com/questions/36915133/nstimer-inside-global-queue-is-not-called-when-the-app-is-in-the-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!