Reputation: 978
I am new to NSURLSession and specially background download tasks. Is there a way to query NSURLSession to return a list of background tasks? It seems possible to make duplicate requests for background download tasks. I do not get any error if I make the same url download request while a previous one is still in progress.
What is the best way to handle this situation? I can maintain a list of urls I am currently downloading from, but in case the app is relaunched I lose this reference. I can again store this information in persistent storage. But it just seems inconvenient not to have an option to query NSURLSession for this.
Upvotes: 4
Views: 1849
Reputation: 30746
Apparently if you use the same session and same request then URLSession will deduplicate the requests automatically. This was said at WWDC:
"This code is not making the underlying network request twice as we're using the same URLSession to back both, and URLSession will deduplicate any in-process requests under the hood." [WWDC 2022 Efficiency awaits: Background tasks in SwiftUI 11:26]
However, I was unable to find it mentioned in the docs.
Upvotes: 1
Reputation: 17478
You could get the list of all download tasks added to the session with the following call.
[[self defaultSession] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
}];
Upvotes: 6