用AFNetworking计算下载多个文件的总进度

时间:2014-09-07 13:57:16

标签: ios objective-c download afnetworking

我想下载多个文件,然后向用户显示总进度。

但问题出在这里我不知道应该如何计算总进度。

这是我的所作所为: 首先我得到totalBytes预期从所有文件中收到:

for (NSURL candidateUrl in UrlsList)
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:candidateURL];
                        //setting HTTPMethod of request from GET to HEAD to download header files of requests, so we can get file size before downloaing file
                        [request setHTTPMethod:@"HEAD"];

                        getTotalImagesBytesOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                        [getTotalImagesBytesOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                         {
                             //setting totalImagesBytesExpectedToRead of all images. we use it to know how many bytes we should download for all the images
                              totalImagesBytesExpectedToRead += [operation.response expectedContentLength];


                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             NSLog(@"Error: %@", error);
                         }];

                        [operationQueue addOperation:getTotalImagesBytesOperation];
}
估算总文件大小后

        //downloading images
    for (NSURL *imageUrl in imagesURLList) {
        NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];
        AFImageRequestOperation *downloadImageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:request
                                                                                  imageProcessingBlock:nil

                                                                                               success: ^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {                                                                                      
NSLog(@"success")
}
failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                                                                       NSLog(@"%@", [error localizedDescription]);
                                                                                                   }];

        [operationQueue addOperation:downloadImageOperation];

        [downloadImageOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
        {

          **HOW TO CALCULATE TOTAL PROGRESS**
}

我不知道如何计算总大小!! 我们拥有的价值观: 以上方法为您提供的当前文件的totalBytesOfAllTheFiles,totalBytesRead和totalBytesExpectedToRead,indexOfCurrentFile和countOfFiles。

谨慎的setDownloadProgressBlock一次调用了几百次。

有没有人有任何想法? (对不起代码格式错误!)

2 个答案:

答案 0 :(得分:4)

以下是我实现的目标: 首先创建一个NSOperationQueue:

// Add the operation to a queue
// It will start once added
//calculating images byte size
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:1];
foreach (NSURL *candidateURL in urlList )
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:candidateURL];
                    //setting HTTPMethod of request from GET to HEAD to download header files of requests, so we can get file size before downloaing file
                    [request setHTTPMethod:@"HEAD"];

                    AFHTTPRequestOperation *getTotalImagesBytesOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                    [getTotalImagesBytesOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                     {
                         //setting totalImagesBytesExpectedToRead of all images. we use it to know how many bytes we should download for all the images
                         totalImagesBytesExpectedToRead += [operation.response expectedContentLength];


                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                         NSLog(@"Error: %@", error);
                     }];

                    [operationQueue addOperation:getTotalImagesBytesOperation];
}

 //downloading images which should be downloaded
for (NSURL *imageUrl in imagesShouldBeDownlaoded) {
    NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];

    AFHTTPRequestOperation *downloadFileOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    //we must provide file path with file name and its extension
    NSString *fileName = [self getImageName:[imageUrl absoluteString]];
    downloadFileOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:[[ImageManager applicationDocumentsDirectory] stringByAppendingPathComponent:fileName] append:NO];

    [downloadFileOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation , id response)
     {
         NSLog(@"file saved");

     }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         completionBlock(NO, error);
         NSLog(@"%@", [error localizedDescription]);
     }];

    [operationQueue addOperation:downloadFileOperation];

    [downloadFileOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
     {
         totalBytesDownloaded += bytesRead;

         //total progress from 0.0 to 1.0
         progress = ((float)totalBytesDownloaded/(float)totalImagesBytesExpectedToRead);
         if(progress == 1)
         {
             completionBlock(YES, nil);
         }
         progressCallback(progress);
    }];

operationQueue作为FIFO列表,首先计算图像字节大小,然后开始下载所有图像

答案 1 :(得分:0)

AFNetworking3使用dispatch_groupNSProgress实现了这一目标:

#import <Foundation/Foundation.h>
#import <AFNetworking.h>

@interface Server : NSObject

@property (nonatomic, strong) AFHTTPSessionManager *manager;
@property (nonatomic, strong) NSProgress *progress;

- (void)downloadFilesWithUrls:(NSMutableArray <NSURL *>*)urls;

@end



#import "Server.h"
@implementation Server

- (instancetype)init
{
    self = [super init];
    if (self) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];

        _progress = [NSProgress progressWithTotalUnitCount:0];
        [_progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"fractionCompleted:%lf",self.progress.fractionCompleted);
}

- (void)downloadFilesWithUrls:(NSMutableArray <NSURL *>*)urls {
    //progress
    dispatch_group_t group = dispatch_group_create();
    _progress.totalUnitCount = urls.count;
    [urls enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         dispatch_group_enter(group);
        NSURLRequest *request = [NSURLRequest requestWithURL:obj];
        NSURLSessionDownloadTask *task = [self.manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            return [[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil] URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            NSLog(@"one task completion");
            dispatch_group_leave(group);
        }];

        NSProgress *child = [self.manager downloadProgressForTask:task];
        [self.progress addChild:child withPendingUnitCount:1];
        [task resume];
    }];

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"%lf", self.progress.fractionCompleted);
    });

}

@end