2

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?

lorak
  • 113
  • 1
  • 6

1 Answers1

2

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.

Mohamed Mostafa
  • 1,057
  • 7
  • 12
  • w.r.t your line *iOS provides a 30 seconds time frame in order the app to be woken up*, Is there any written doc from Apple that it will always provide 30 seconds for the app? – Rajan Maheshwari May 30 '17 at 20:05
  • @RajanMaheshwari as mentioned by Apple When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block.https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623125-application – Mohamed Mostafa Jun 01 '17 at 01:55