I've been doing some thorough investigations into setting up a solid background download process. Here are some of the gotchas I stumbled upon and wanted to capture:
- Only download and upload tasks are allowed to run in the background (no data tasks)
- Once a download task completes, your app will have to move that task from its temporary location to a permament location (or process the data somehow); The temporary file will be deleted once the
URLSession:downloadTask:didFinishDownloadingToURL:
delegate method returns.
- If your app is suspended, it will be re-launched whenever a background task completes
- If your app is (re-) launched through a completed background task, you will have to create a NSURLSession instance with the same identifier as passed into
application:handleEventsForBackgroundURLSession:completionHandler:
and assign a delegate to receive individual download task delegate messages
- If a user force-quits your app (double-tapping home and swiping up), your download tasks will be cancelled
didCompleteWithError:
will be called if a task completes successfully or with an error- If a task is cancelled (manually or by force quitting) then
task.error.code
will have the valueNSURLErrorCancelled
, buttask.state
will beNSURLSessionTaskStateCompleted