Используя AFNetworking 3.0, Как я могу скачать серию файлов по порядку?

Итак, я уже несколько дней обыскиваю эти форумы и другие сайты и не могу найти подходящую замену старому способу загрузки серии файлов в AFNetworking 1.0 и 2.0.

Я пытаюсь использовать NSURLSessionDownloadTask для загрузки этих файлов, но я знаю, что они не похожи на потоки, поэтому вы не можете рассматривать их как таковые.

Ранее я мог сделать это для загрузки нескольких файлов по порядку и получения обновлений о ходе выполнения всей операции (не только для каждого файла, но и для всей очереди).

Вот основная структура, которую я использовал...

 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];

Я думаю, что в AFNetworking 1.0 это называлось "пакетным" запросом, но я знаю, что он больше не используется. Я пытаюсь адаптировать новые NSURLSessions для загрузки этих файлов.

Любая помощь будет оценена!

0 ответов

Другие вопросы по тегам