Skip to content

Instantly share code, notes, and snippets.

@benjaminbojko
Last active August 10, 2019 00:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminbojko/7494210c04e0826a2ee1 to your computer and use it in GitHub Desktop.
Save benjaminbojko/7494210c04e0826a2ee1 to your computer and use it in GitHub Desktop.
iOS Background Download Gotchas

iOS Background Download Gotchas

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:

Background Tasks

  • 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.

App Suspension

  • 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

Cancelled Tasks

  • 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 value NSURLErrorCancelled, but task.state will be NSURLSessionTaskStateCompleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment