3

I'm trying to implement a seemingly common feature in my app: to upload files in the background. I run such tasks by calling -[UIApplication beginBackgroundTaskWithExpirationHandler:], so those tasks are each identified by a UIBackgroundTaskIdentifier.

Please take a look at the diagram below. Orange boxes are problem points for which I can't seem to find any answer.

enter image description here

Here are the questions for which I seek guidance:

Is it possible to get a list of running tasks that same app initiated in a previous session?

Is it possible to associate some kind of meta data, such as a URL string, with a background task, so we can know which task is uploading which file?

jscs
  • 63,694
  • 13
  • 151
  • 195
craftsman
  • 15,133
  • 17
  • 70
  • 86
  • As written, your question is too vague. Are you talking about running on a background thread, using NSThread, NSOperations, or GCD queues? Or are you talking about asking for background time when the user swaps your app to the background? – Duncan C May 26 '14 at 19:14
  • I want the OS to continue uploading the files, even when the app is closed or sent to background. – craftsman May 26 '14 at 19:26
  • See Rob's answer. You want NSURLSession. It lets you handle uploads/downloads even when your app is in the background. – Duncan C May 26 '14 at 19:37

1 Answers1

3

Are you using NSURLConnection or NSURLSession?

If NSURLSession, you can use getTasksWithCompletionHandler. You can also use a background NSURLSessionConfiguration, rather than relying on beginBackgroundTaskWithExpirationHandler. And it terms of keeping track of the requests, you can retrieve the originalRequest from the task (and retrieve the URL from that), or update your model with the taskIdentifier for the NSURLSessionTask, and cross reference requests in your own model that way.

Rob
  • 415,655
  • 72
  • 787
  • 1,044