I have a timer when the device enters the background, since I want to save the check on a small amount of data in my service. I am using the following code in the applicationDidEnterBackground method in the application delegate
UIApplication *app = [UIApplication sharedApplication]; //create new uiBackgroundTask __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //and create new timer with async call: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //run function methodRunAfterBackground NSString *server = [variableStore sharedGlobalData].server; NSLog(@"%@",server); if([server isEqual:@"_DEV"]){ arrivalsTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(getArrivals) userInfo:nil repeats:YES]; } else { arrivalsTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(getArrivals) userInfo:nil repeats:YES]; } [[NSRunLoop currentRunLoop] addTimer:arrivalsTimer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; });
This works absolutely fine until the device closes automatically and then the timer stops ticking. Any suggestions on how to stop this? The default time is 5 minutes, so most devices will be locked long before it is checked even once.
thanks
source share