Question

After upgrading my app using AFNetworking 1.3 to 2.0, now it seems that downloading the same files took a little bit longer. This is log of the download of 4 files on a batch operation using AFNetworking 1.3.x:

2014-03-21 19:29:23.775 <my_app>[2056:60b] 4 / 4
2014-03-21 19:29:23.777 <my_app>[2056:60b] 4 / 4
2014-03-21 19:29:23.778 <my_app>[2056:60b] 4 / 4
2014-03-21 19:29:23.779 <my_app>[2056:60b] 4 / 4

and this is the log of the same files using AFNetworking 2.0

2014-03-21 19:31:15.616 <my_app>[2068:60b] 1 of 4 complete
2014-03-21 19:31:15.876 <my_app>[2068:60b] 2 of 4 complete
2014-03-21 19:31:16.065 <my_app>[2068:60b] 3 of 4 complete
2014-03-21 19:31:17.244 <my_app>[2068:60b] 4 of 4 complete

As you can see, using v1.3.x was a lot faster than using 2.0. Below is the code that I'm using on v2.0:

NSMutableArray *tempOperations = [[NSMutableArray array] init];
NSArray *tempUrls = [[NSArray alloc] initWithObjects: obj1, obj2, obj3, obj4, nil];

for (id urlTemp in tempUrls){
    //Create NSURLRequest
    NSURL *url = [[NSURL alloc] initWithString:urlTemp];
    NSMutableURLRequest *finalrequest = [[NSMutableURLRequest alloc] initWithURL:url];

    // AFNetworking Request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:finalrequest];
    [operation setResponseSerializer:[AFHTTPResponseSerializer new]];

    // Add request to array
    [tempOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:tempOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    for (AFHTTPRequestOperation *ro in operations) {

        if (ro.error) {

            NSLog(@"++++++++++++++ Operation error: %@", ro.error);

        }else {

            NSLog(@"Operation OK: %@", [ro.request URL]);

        }
    }
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
Was it helpful?

Solution

Well, I found a solution, not the way it was working before, but I'm getting better results. The change is that a create an instance of AFHTTPRequestOperationManager using initWithBaseURL and that seems to improve request's speed.

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"<url>"]];
[manager setResponseSerializer:[AFHTTPResponseSerializer new]];

for (id urlTemp in tempUrls){
    [manager GET:urlTemp parameters:nil success:^(AFHTTPRequestOperation *ro, id responseObject) {
        NSLog(@"Operation OK: %@", [ro.request URL]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top