0

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!

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
Airtower
  • 255
  • 1
  • 3
  • 10
  • Why don't you use your own operation queue with a concurrent max of 1? – Wain Jan 02 '16 at 22:44
  • I am considering that because I have done that in the past but then how would I track the progress of each file downloading? – Airtower Jan 02 '16 at 22:47
  • Each individual download gives a set percentage of the whole by the number of operations in total – Wain Jan 02 '16 at 22:50
  • Yes. I realized I worded my response wrong. What I meant was that I want to get the total amount of data to download from the queue, then start the download and move the progress bar for each progress update returned until I get all the files done. This is not really possible in my own operation queue. i would merely be returning the progress of the amount of file and not the amount of data downloaded in total – Airtower Jan 02 '16 at 22:51
  • Yes, you would need to send a HEAD request to each endpoint to get the data size, a lot of servers won't implement HEAD so will just do a GET instead. – Wain Jan 03 '16 at 09:18

0 Answers0