Wayne
Wayne

Reputation: 508

How to use AFNetwork download multiple files

I wanna use AFNetwork to download multi files, but I have no idea how to implement this? As you see, I create a operation array and add 3 tasks in it

NSMutableArray *operations = [NSMutableArray array];
NSArray *requestArray = @[ @"...task1.zip", @"task2.zip", @"task3.zip" ];
for (int i = 0; i < 3; i++) {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[requestArray objectAtIndex:i]]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[[requestArray objectAtIndex:i] lastPathComponent]];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"Operation%i: bytesRead: %d", i, bytesRead);
        NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesRead);
        NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesExpectedToRead);
    }];
    [operations addObject:operation];
}

And then, what should I do? I do something following, but nothing happens

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];
[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

} completionBlock:^(NSArray *operations) {
}];

Any ideas about this problem?

Upvotes: 1

Views: 3118

Answers (3)

Praveen Sevta
Praveen Sevta

Reputation: 149

Try this...

NSArray *linkArr = [NSArray arrayWithObjects:@"http://abc.mp3",@"http://abc.mp3",@"http://abc.mp3", nil];

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

    for (int i=0; i< linkArr.count; i++) {

        NSString *urlpath = [linkArr objectAtIndex:i];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Audio%i.mp3",i]];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Successfully downloaded file to %@", path);
            // Init the audio player.


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

        [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);


        }];



        [operationQueue addOperation:operation];
    }

Upvotes: 0

alex_kael
alex_kael

Reputation: 61

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init]; should be replaced with AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]]; because initWithBaseURL init a lot of things.

Upvotes: 5

ubaltaci
ubaltaci

Reputation: 1016

[operation start];

or NSOperationQueue is what you looking for.

Upvotes: 0

Related Questions