So, I have been scouring these forums and other sites for a few days now and can not find a suitable replacement for the old way to download a series of files in AFNetworking 1.0 and 2.0.
I am trying to use NSURLSessionDownloadTask to download these files but I know they aren't like threads so you can't treat them as such.
Previously I could do this to download multiple files in order and get progress updates on the total operation (not just on each file, but on the entire queue).
Here is the basic structure I was using...
for(Photo *photo in array){
//form the path where you want to save your downloaded image to
NSString *constPath = [photo imageFullPath];
//url of your photo
NSURL *url = [NSURL URLWithString:photo.serverPath];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
op.responseSerializer = [AFImageResponseSerializer serializer];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:constPath append:NO];
op.queuePriority = NSOperationQueuePriorityLow;
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){
}];
op.completionBlock = ^{
//do whatever you want with the downloaded photo, it is stored in the path you create in constPath
};
[requestArray addObject:op];
}
NSArray *batches = [AFURLConnectionOperation batchOfRequestOperations:requestArray progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {
//after all operations are completed this block is called
if (successBlock)
successBlock();
}];
[[NSOperationQueue mainQueue] addOperations:batches waitUntilFinished:NO];
(this code was taken from another post but it is exactly what i was using on a previous app)
And now, trying to use NSURLSessionDownloadTask, I can not find a way to adapt the following code to run in a series (1 file at a time, in order, with progress and file data along the way).
NSURLSessionDownloadTask *newTask = [_downloadReqOps downloadTaskWithRequest:myRequest progress:^(NSProgress * _Nonnull downloadProgress) {
//Progress
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [CompanyInfo storeFileInFolder:kTempFiles], response.URL.absoluteString.lastPathComponent];
//NSLog(@"File Path: %@", filePath);//response.URL.absoluteString.lastPathComponent);
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
}];
[newTask resume];
I think this used to be called a "batch" request in AFNetworking 1.0 but I know that this is not used any more. I am trying to adapt the new NSURLSessions to download these files.
Any help would be appreciated!