I have some long running process which I want to run even if application goes in background. I am calling application's beginBackgroundTaskWithExpirationHandler:
method and in the expirationBlock I am calling application's endBackgroundTask
.
Here is the implementation:
__block UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
dispatch_queue_t queue = dispatch_queue_create("com.test.test1234", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
// My Task goes here
});
In some cases, my serial queue have more task to perform which can not be completed within the time provided by system. So expiration block will execute and in that I am ending the UIBackgroundTaskIdentifier
but not stopping the dispatch process (I can't even cancel a dispatch).
Apple's document says:
Each call to the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method generates a unique token to associate with the corresponding task. When your app completes a task, it must call the endBackgroundTask: method with the corresponding token to let the system know that the task is complete. Failure to call the endBackgroundTask: method for a background task will result in the termination of your app. If you provided an expiration handler when starting the task, the system calls that handler and gives you one last chance to end the task and avoid termination.
So, according to this if I doesn't call the endBackgroundTask:
my app will be terminated, which is ok.
My question is: with my current implementation what if I call endBackgroundTask:
in expirationHandler block and my dispatch queue's task doesn't get complete? My app will be terminated or will be suspended?
thanks
endBackgroundTask
right? – Josefajosefina