Ios background download url session 10 mins






















The example also shows how to implement progress monitoring for multiple tasks running in parallel:. To start a download that can be completed in background without the app running, create a URLSessionConfiguration for background processing. Caution: If there already exists an URLSession with the same id, it doesn't create a new URLSession object but returns the existing one with the old delegate object attached. So make sure you only create the session once or handle this accordingly.

You have your background session, and you can use it to create your download tasks and upload tasks as well. We create a url and use the session to create a download task. This will allow the system to optimise our download. We can also set the earliest date of the transfer. In our example, it can start 2 minutes from now, or 20 minutes from now. When your download finishes, your AppDelegate will get notified.

Your code will probably look identical to this one:. You will use this completion handler from our controller. Speaking of which. Remember when we registered as a delegate of that URLSession. Well, when the download completes our delegate methods will get called next, you only need to implement one function:. We also implemented the session download delegate, in order to get the location of the temp file and the download progress:. It might make sense to keep your Xcode debugger attached, move your app in the background, waiting for the upload to continue.

If you like, you can always detach and then reattach later on. Just make sure to detach and allow your app to suspend. Note that your app will only continue after all running tasks succeeded. More about that later! As a good replacement of the Xcode debugger, you can make use of OSLog. While keeping the Console app open, you can start running URLSession upload tasks and move your app into the background. The more detailed your logging implementation is, the easier it will debug flaws in your uploading logic.

This gets me to the next point:. This delay in resuming your app likely enlarges when requesting more and more uploads. Therefore, it might be time-consuming to test your flows. Lastly, your app will only be relaunched after all running background tasks succeed. How better to end this list with a few tips on speeding up your testing flow for background tasks. We can do this by executing the following piece of code on launch to invalidate our URLSession and its tasks:.

Next, we spawn a new thread and perform our long-running task. Note that the EndBackgroundTask call is now made from inside the long-running task, since the DidEnterBackground method will have already returned.

Kicking off tasks to run in the background allows DidEnterBackground to return in a timely manner, keeping the UI responsive and preventing the watchdog from killing the application. By keeping track of the remaining backgrounding time, and using expiration handlers when necessary, we can avoid iOS terminating the application.

If an application with registered tasks gets moved to the background, the registered tasks will get about seconds to run. We can check how much time the task has to complete using the static BackgroundTimeRemaining property of the UIApplication class.

The following code will give us the time, in seconds, that our background task has left:. In addition to giving access to the BackgroundTimeRemaining property, iOS provides a graceful way to handle background time expiration through an Expiration Handler. This is an optional block of code that will get executed when the time allotted for a task is about to expire. Code in the Expiration Handler calls EndBackgroundTask and passes in the task ID, which indicates that the app is behaving well and prevents iOS from terminating the app even if the task runs out of time.

EndBackgroundTask must be called within the expiration handler, as well as in the normal course of execution. The expiration handler is expressed as an anonymous function using a lambda expression, as illustrated below:. While expiration handlers are not required for the code to run, you should always use an expiration handler with a background task. The biggest change in iOS 7 with regard to background tasks is not how the tasks are implemented, but when they run.

Recall that pre-iOS 7, a task running in the background had seconds to complete. One reason for this limit is that a task running in the background would keep the device awake for the duration of the task:.



0コメント

  • 1000 / 1000