Reputation: 934
If I have a large file download going on an the app gets moved to background, is there any way to keep the download executing functions alive?
I knowbeginBackgroundTaskWithExpirationHandler:
gets called when the app moves to the background and I can start my task there, but I don't want to start a new task, I want to complete my old task. It can be solved with beginBackgroundTaskWithExpirationHandler:
, but for that I need to pause my download and resume it from the right place, which is just plain silly.
Ideally what I want is that I wrap my download function with an expiration handler, so my download function keeps executing for the permitted time after the app has been moved to the background.
Upvotes: 6
Views: 1590
Reputation: 299265
Ideally what I want is that I wrap my download function with an expiration handler, so my download function keeps executing for the permitted time after the app has been moved to the background.
This is exactly how it works. beginBackgroundTaskWithExpirationHandler:
is not called when you enter the background. It's what you call to indicate that you're starting something that, if you happen to go into the background while it's running, you would like to finish. Just wrap your existing download code with beginBackgroundTaskWithExpirationHandler:
and endBackgroundTask:
.
Upvotes: 7
Reputation: 4702
It is perfectly fine to start a background thread when you're in the forground. Add your custom expiration handler. Do an asynchronous request.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[self performYourStuffInTheBackGround];
});
And a quote from AppleDEV;
you will probably want to use asynchronous network APIs. iOS provides a wide variety of APIs to do this—from low-level APIs like GCD to high-level APIs like NSURLConnection, with many stops in between—and we encourage you to use them.
Upvotes: -3