Reputation: 951
How can I keep track of multiple downloads with NSURLSession
?
For example:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *jsonTask = [session downloadTaskWithURL:[NSURL URLWithString:[urlString stringByAppendingString:@"iOSMenu.json"]]];
NSURLSessionDownloadTask *imageTask = [session downloadTaskWithURL:[NSURL URLWithString:[urlString stringByAppendingString:@"[email protected]"]]];
NSURLSessionDownloadTask *titleTask = [session downloadTaskWithURL:[NSURL URLWithString:[urlString stringByAppendingString:@"[email protected]"]]];
[jsonTask resume];
[imageTask resume];
[titleTask resume];
I downloaded these three files and I can confirm it with NSLog
, like this.
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSLog(@"%@", location);
}
But I don't know how can I access and differentiate these files. How can I do that?
Upvotes: 3
Views: 1096
Reputation: 438212
The didFinishDownloadingToURL
method passes you a reference to the NSURLSessionDownloadTask
in that delegate method. From that, you can either refer to the task's taskIdentifier
or refer to the task's originalRequest.URL
property.
You want to make sure you move the file at location
to somewhere you'll have access to in the future (as when you return from didFinishDownloadingToURL
, it will remove that temporary file if you don't do something with it).
For example, you might save the file to your documents folder. In this example, I'll grab the last path component of the original URL, create a path to a file in your documents folder, and move the file to new location:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:[downloadTask.originalRequest.URL lastPathComponent]];
NSURL *newLocation = [NSURL fileURLWithPath:path];
NSError *error;
if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:newLocation error:&error]) {
NSLog(@"failed to move %@ to %@: %@", location, newLocation, error);
}
}
Alternatively, if you want to not rely upon the lastPathComponent
of the original URL, you can maintain a dictionary mapping the task identifiers (or original URLs) to your new location you want to save the file. You can build that dictionary as you create the tasks, and then the didFinishDownloadingToURL
could look up the desired destination using the task's identifier in that dictionary, and use that in the moveItemAtURL
method. Clearly, if this is a background session, you'll want to make sure you save this cross reference to persistent storage so you can re-retrieve it when the app is restarted when the downloads finish.
Upvotes: 1