IOS: task completion when entering background

If I have a large file download and the application moves to the background, is there a way to preserve the download execution features?

I know that beginBackgroundTaskWithExpirationHandler: is called when the application moves to the background, and I can run my task there, but I do not want to start a new task, I want to complete the old task. It can be solved with beginBackgroundTaskWithExpirationHandler: but for this I need to pause the download and resume it from the right place, which is just plain stupid.

Ideally, I want me to load the download function with an expiration handler, so my download function continues to run for the allowed time after the application has been moved to the background.

+6
source share
2 answers

Ideally, I want me to load the download function with an expiration handler, so my download function continues to run for the allowed time after the application has been moved to the background.

That is how it works. beginBackgroundTaskWithExpirationHandler: not called when a background is entered. This is what you call to indicate that you are starting something, that if you accidentally go into the background while it is running, you would like to finish. Just wrap the existing boot code beginBackgroundTaskWithExpirationHandler: and endBackgroundTask:

+6
source

It is perfectly normal to start a background thread when you are on the field. Add your custom expiration handler. Make 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 probably want to use asynchronous network APIs. iOS provides many APIs for this, from low-level APIs such as GCDs to high-level APIs such as NSURLConnection, with many stops between them, and we recommend that you use them.

-3
source

Source: https://habr.com/ru/post/924525/


All Articles