ios - AFNetworking - no response on all but the final request in a for loop -
i've tried several variants of afnetworking classes, , 1 example, in uiimageview+afnetworking, instance, cannot make requests in loop , more 1 frame back. meaning, don't understand why ever last response comes back, in success block, others sent out hang indefinitely:
for (unsigned int = 1; <= numframes; ++i) { // fire request nslog(@"request made frame #%u", i); // ith frame nsstring* urlstring = [nsstring stringwithformat:@"%@%u.png", urlprefix, i]; nsurl* frameurl = [nsurl urlwithstring:urlstring]; nsurlrequest* request = [[nsurlrequest alloc] initwithurl:frameurl]; [_myimage setimagewithurlrequest:request placeholderimage:spinnerimage success:^(nsurlrequest* request, nshttpurlresponse* response, uiimage* image) { if (image != nil) { nslog(@"frame %u done!", i); frames[i - 1] = image; } else { nslog(@"frame #%u nil, expected %lld bytes!", i, response.expectedcontentlength); } } failure:^(nsurlrequest* request, nshttpurlresponse* response, nserror* error) { nslog(@"reason: %@ - %@", error, [error userinfo]); }]; }
the output in console is
2016-03-06 20:04:45.795 [2625:383915] reachability flag status: -r ------- networkstatusforflags 2016-03-06 20:04:45.825 [2625:383915] request made frame #1 2016-03-06 20:04:45.833 [2625:383915] request made frame #2 2016-03-06 20:04:45.834 [2625:383915] request made frame #3 2016-03-06 20:04:45.921 [2625:383915] [crashlytics] version 3.7.0 (102) 2016-03-06 20:04:46.388 [2625:383915] reachability flag status: -r ------- networkstatusforflags 2016-03-06 20:04:46.389 [2625:383915] reachability flag status: -r ------- networkstatusforflags 2016-03-06 20:04:46.389 [2625:383915] reachability flag status: -r ------- networkstatusforflags 2016-03-06 20:04:46.468 [2625:383915] frame 3 done!
if change numframes 4, or 10, or 50, same thing happens: request placed every frame, last 1 ever finishes. other ones don't return anything, either nil image in success block, or error in failure block.
my question is, doing wrong? or better yet, in way can make bunch of frame requests (not sequentially or synchronously) , have them return without hanging? i've checked network reachability , actual urls, , normal there. there has way make bunch of requests @ once, , hear of them when url , network accessibility check out ok.
i have tried few things:
(1) waiting long time
(2) checking timeout , errors
(3) putting request second frame in success block of first (and second request gets made, doesn't hear back)
(4) other versions of afnetworking
with no luck!
there has better api pulling frames network, because it's common operation when putting animation...
your code looks fine, try using code downloading images, i'm able use in loops successfully. thats can now.
nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url]; afhttprequestoperation *requestoperation = [[afhttprequestoperation alloc] initwithrequest:urlrequest]; requestoperation.responseserializer = [afimageresponseserializer serializer]; [requestoperation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { [imageview setimage: responseobject]; } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"image error: %@", error); }]; [requestoperation start];
Comments
Post a Comment