问题
My application uses a backgroud fetch to send and upload a small portion of data every 30 minutes. The service is working correctly for about 10 - 14 hours after the application is minimized from working in the foregroud - the application is correctly sending and receiving the data every 30 minutes.
Does anyone know what happens with the service after couple of hours? Does the iOS system automatically terminate the application and therefore the background fetch stops working?
Can anyone explain?
回答1:
iOS provides a 30 seconds time frame in order the app to be woken up, fetch new data, update its interface and then go back to sleep again. It is your duty to make sure that any performed tasks will manage to get finished within these 30 seconds, otherwise the system will suddenly stop them.
maybe the internet was slow that your app took more than 30 seconds & system stopped your application.
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSDate *fetchStart = [NSDate date];
// Make your process here
[viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
NSDate *fetchEnd = [NSDate date];
NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}];
}
Also Apple provides an algorithm which defines how often the background fetch should trigger, based on your own usage of the app. If you use it a lot, then it will fetch as often as possible, but if you use like at 4pm every day, the background fetch should trigger just before, so your data is updated when you launch it.
来源:https://stackoverflow.com/questions/32298681/background-fetch-stops-working-after-about-10-to-14-hours