ioscocoa-touchuibackgroundtask

How can I know which tasks are running in the background?


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?


Solution

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