Search code examples
iosnsurlsessionnsurlsessiondownloadtask

NSURLSession background task: avoid duplicates


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.


Solution

  • 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) {
    
    }];