Reputation: 11
I have a timer in the app. To reproduce the problem, user should start this timer, after that enter app to background or turn off the screen. The app works on up to 5 mins and then killed by the system. After users turn screen back on, they can see home screen. And if to start the app again, it starts from the beginning, but not from the place where user was before turning screen off.
I try to solve this problem with the following solution in my AppDelegate:
- (void)methodToRepeatEveryOneSecond
{
if (self.flag) {
dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, q_background, ^(void){
[self methodToRepeatEveryOneSecond];
});
}
}
-(void)applicationDidEnterBackground:(UIApplication *)application{
NSUserDefaults* def = [[NSUserDefaults alloc] init];
if ( [[def valueForKey:@"status" ] boolValue]) {
self.flag = true;
dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(q_background, ^{
[self methodToRepeatEveryOneSecond];
});
}
}
-(void)applicationWillEnterForeground:(UIApplication *)application{
self.flag = false;
}
But app still stops working after ~5min in background.
When I'm testing this solution with help of USB cable app do works in the background even up to 30 mins, but after about 4 min of working in the background I can see the following logs in the console (here is a part of it) :
2017-07-26 17:43:14.496379+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.496493+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e1b6530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.496790+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e012530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497065+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497188+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497476+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
But if to test the same build with help of downloading from the testflight, I get the problem, that I wrote above (with showing of home screen).
Also I tried use support for location in the UIBackgroundModes key in my Info.plist file, it worked correct, but beta app review was rejected with a problem of «Guideline 2.5.4».
Please, give me advice, what I can use to solve this problem
Upvotes: 0
Views: 233
Reputation: 2099
when the app is in the background your app terminates itself 3 minutes of inactivity and you loose all your previliges and your app cant do anything but listen get notifications (if you have notifications implemented)
If you are trying to forbid you app to terminate, you can let it run a silent mp3 in that background that way your app is always active (drains battery plus will get rejected).
But if the user double tap home button and swipe up your app (terminates it). There is nothing you can do!
Hope this helps!
Upvotes: -1