Check if Background URLSession with identifier exists

I have a working background URLSession. I know that upon creating an URLSession with the same session identifier I get a "background URLSession with identifier x already exists" message.

I know that I can store the session and call .finishTasksAndInvalidate() on it if needed.

My use case is that if the application terminates, and the user relaunches the application before the background task completes, I need to be able to check if a background URLSession with the same identifier exists, and if it does, restitute the application state with the same handlers (so that I can update a UIProgressView for example).

I have two questions:

  1. How do I check that a background URLSession with a given identifier already exists?
  2. Does the AppDelegate completion handler still get called if the application was terminated and relaunched?

Accepted Reply

Yes it's a single hard-coded identifier.

OK, good.

So then why do you want to check whether the session exists? Just connect up to the session on launch and call getAllTasks(completionHandler:) to determine if any tasks are running in the session. The existence of the tasks is what matters here, not the session.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

OK, I need to clarify your architecture here. How are you allocating background session identifiers? Most apps can get away with using a single hard-coded identifier, or maybe a small set of them. Are you doing that? Or are you allocating the identifies dynamically? And, if so, why?

I see a lot of folks allocating background session identifiers — using a UUID, say — because they think it’s the right thing to do. And it can be the right thing to do in very limited circumstances, but in most cases it just needlessly complicates things.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes it's a single hard-coded identifier.

Yes it's a single hard-coded identifier.

OK, good.

So then why do you want to check whether the session exists? Just connect up to the session on launch and call getAllTasks(completionHandler:) to determine if any tasks are running in the session. The existence of the tasks is what matters here, not the session.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks, that's what I was looking for.

Hi @thanat0sis I have same kind of situation with different use case. Can you please help me if you know any solution.

  • If I have one AF session with 'DataRequest` for a url http://xyz1.com/profile is already called in AF session
  • same time some other thread also called same above url same time
  • both are called async so after completion of their individual task, then only they will get json data to parse and update UI.
  • My question here is :- If some instance of viewModel has called url "http://xyz1.com/profile" and some other viewModel also called same url same time, so AF should discard/cancel the new API request for same url (since that is already hit for getting data from remote).. but once completion is called for first API request and data saved on local (we support caching) then it should notify the other place for which the API call was cancelled so that other viewModel should get notified that so that the other viewModel reload self from Cache as remote API for the same done and cache is updated.

I hope you got the question. Kindly guide.

thanks in advance.