Ellis
Ellis

Reputation: 699

iOS: when does end background task code block get invoked?

I have a long running task that I want completed, even if the application gets pushed to the background. From the documentation I can see that the way to do this is to use the beginBackgroundTaskWithExpirationHandler: feature and asynchronously start a task running as in the following code snippet:

UIApplication* application = [UIApplicationsharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you.
    // stopped or ending the task outright.

    [application endBackgroundTask: bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task, preferably in chunks.
         for (int i = 0; i < 3; i++){

        [self doChunkOfWork];
    }

    // end work
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

Now I understand that the second code block gets executed asynchronously and once completed the application is notified and the tasks is marked as invalid, however, can anyone tell me under what circumstances the first code block gets executed and also how the first and second blocks get associated? Is it simply that because I have started a task and sequentially the next thing I do is call dispatch_async that the two blocks will be associated?

Upvotes: 4

Views: 4025

Answers (1)

Phil Calvin
Phil Calvin

Reputation: 5105

These blocks are not related in any "magic" way.

The second block will be queued and run asynchronously, as you state. The first block will only ever be called by the system if:

  1. The application is sent to the background (i.e., the user switches to another app)
  2. Your second block's [self doChunkOfWork] blocks for more than about 10 minutes (on current iOS versions).

If -doChunkOfWork blocks, [application endBackgroundTask:bgTask] will never be called, and the system will—rightly!—think you're still doing work. iOS sets a limit on the amount of time you can work in the background, and it'll call your expiration handler (the first block) if you exceed this limit.

Your current implementation for the expiration handler immediately marks the task as finished. At that point, your application would no longer be executing, and if doChunkOfWork is still going, it will be paused. (In fact, all of your application's threads will be paused.)

At some future time, the user may switch back to your app. Your threads will then be resumed, and doChunkOfWork will continue executing right where it was.

Upvotes: 5

Related Questions