ios - Timeout when issuing too many AFNetworking requests -
i have code download 40 json
nsmutablearray *mutableoperations = [nsmutablearray array]; (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 can see use manager have queue of request. problem suddenly, go in timeout -1001 code. happens in edge mode, in wifi , 3g don't happen.
what's problem?
if specify maxconcurrentoperationcount
of operation queue, control how many concurrent operations attempted, mitigating timeouts resulting fact ios limits how many simultaneous network connections permitted:
manager.operationqueue.maxconcurrentoperationcount = 4; [manager.operationqueue addoperations:operations waituntilfinished:no];
in absence of this, when submit 40 operations, of them attempt start nsurlconnection
objects, though 4 or 5 can run @ at time. on slow connections, can result in of latter requests timing out.
if specify maxconcurrentoperationcount
, won't attempt start latter connections until prior connections have completed. you'll still enjoy performance benefit of concurrent requests, won't making bunch of requests timeout because of throttling of concurrent nsurlconnection
requests ios enforces.
Comments
Post a Comment