1

I have a background job that runs every 7 hours, it makes an API call that take approximately 1-3 seconds. Looking at the battery section of the setting it’s showing that the job has been running in the background for 40 minutes in the past 20 hours.

simulating background fetch works just fine, and calls completion within seconds.

Any idea why it's running for too long?

NSTimeInterval backgroundFetchTimeInterval = 7 * 3600;
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:backgroundFetchTimeInterval];

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
       [[SyncManager sharedManager] performSyncWithCompletion:^(NSArray *results, NSError *error) {
             if (error) {
                completionHandler(UIBackgroundFetchResultFailed);
            }
            else {
                if (resultCount > 0) {
                    completionHandler(UIBackgroundFetchResultNewData);
                }
                else {
                    completionHandler(UIBackgroundFetchResultNoData);
                }
            }
    }];
}
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Not sure the Battery Settings data is that accurate, but that seems to be quite off the mark. Are you sure you don't have any other background tasks (background notifications, location updates...)? When you perform the fetch, may that trigger updates elsewhere (possibly async, so after you call `completionHandler`), for instance Core Data updates to an `NSFetchedResultsController`? – jcaron Dec 07 '15 at 01:15
  • no background location, I fetch location in the app when needed and I stop listening right after. After completion handler I don't do anything, even if I do it gets terminared. core data updates happen before I call completion :( – aryaxt Dec 07 '15 at 01:42
  • I think this may be happening on development devices, I asked a couple of people who have the app installed and it's not happening on those – aryaxt Dec 07 '15 at 23:37

1 Answers1

1

What you can do is to write in one file how much time you spent.

The idea is to write time when you start the background fetch in the file and the time when you finished it..like this then you can calculate how much really you spent, this will help you to have better analyse.

Mejdi Lassidi
  • 999
  • 10
  • 22