Download one file at a time using afnetworking

久未见 提交于 2019-12-11 19:16:10

问题


I have an array which contains different URLs. I want to download one file with a progress bar, than start the next one and so on.

Here is my code that I have so far;

-(void) func:(NSArray *) arry
{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.timeoutIntervalForRequest = 900;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSMutableArray * downloadedUrl = [[NSMutableArray alloc] init];
    for (NSString * str in arry) {
        NSURL *URL = [NSURL URLWithString:str];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
             NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

             return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

            if(error)
            {
                NSLog(@"File Not Dowloaded %@",error);

            }

        }];
        [downloadTask resume];
    }
}

How would you download one file at a time with a progress bar and then remove the url from array?


回答1:


Declare one global NSMutableArray of file and used that in the function like below.

-(void) downloadFile {

     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
     configuration.timeoutIntervalForRequest = 900;
     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

     NSURL *URL = [NSURL URLWithString:[self.fileArr firstObject]]; //Here self.fileArr is your global mutable array
     NSURLRequest *request = [NSURLRequest requestWithURL:URL];

     NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
     NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

        return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
     } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

        if(error)
        {
            NSLog(@"File Not Dowloaded %@",error);
            [self downloadFile];
        }
        else {
            [self.fileArr removeObjectAtIndex:0];
            if (self.fileArr.count > 0) {
                [self downloadFile];
            }
        }
    }];
    [downloadTask resume];
}

Now call this function but before that initialize self.fileArr and after that call downloadFile method.

Hope this will help you.




回答2:


Give limit the queue to one Operation at a time,

For that, Try to adding dependencies between each operation before you queue them.

If you add dependency between two operations say operation1 and operation2 before adding to queue then the operation2 will not start until operation1 is finished or cancelled.

Do it like:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];

// Make operation2 depend on operation1

[operation2 addDependency:operation1];

[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];

UPDATE

// Create a http operation

NSURL *url = [NSURL URLWithString:@"http://yoururl"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // Print the response body in text

    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}];

// Add the operation to a queue
// It will start once added

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:operation];

hope it help you..!



来源:https://stackoverflow.com/questions/37941306/download-one-file-at-a-time-using-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!