문제

I have this code to download 40 json

NSMutableArray *mutableOperations = [NSMutableArray array];
    for (NSDictionary *dict in general_URL) {

        NSURL *url = [dict objectForKey:@"url"];
        NSString *key = [dict objectForKey:@"key"];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.responseSerializer = [AFHTTPResponseSerializer serializer];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            [self.all_data setObject:[self parseJSONfile:responseObject] forKey:key];

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

        [mutableOperations addObject:operation];
    }

    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        NSLog(@"progress:%f", (float)numberOfFinishedOperations / totalNumberOfOperations);
    } completionBlock:^(NSArray *operations) {

        NSLog (@"all done");

    }];
    [manager.operationQueue addOperations:operations waitUntilFinished:NO];

As you can see I use a manager to have a queue of request. The problem is that suddenly, it go in timeout with -1001 code. It happens only in EDGE mode, in wifi and 3g it don't happen.

What's the problem?

도움이 되었습니까?

해결책

If you specify the maxConcurrentOperationCount of the operation queue, that will control how many concurrent operations are attempted, thus mitigating any timeouts resulting from the fact that iOS limits how many simultaneous network connections are permitted:

manager.operationQueue.maxConcurrentOperationCount = 4;
[manager.operationQueue addOperations:operations waitUntilFinished:NO];

In the absence of this, when you submit your 40 operations, all of them are likely to attempt to start NSURLConnection objects, even though only 4 or 5 can really run at at a time. On slow connections, this can result in some of your latter requests timing out.

If you specify the maxConcurrentOperationCount, it won't attempt to start the latter connections until the prior connections have completed. You'll still enjoy the performance benefit of concurrent requests, but you won't be making a bunch of requests that will timeout because of the throttling of concurrent NSURLConnection requests that iOS enforces.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top