1NSString *BaseUrl;2Nsurl *url =[Nsurl Urlwithstring:[baseurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];3Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten];4Afjsonrequestoperation *jsonoper = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^ ( Nsurlrequest *request, Nshttpurlresponse *response,IDJSON) {5 6 }8failure:^ (nsurlrequest *request, Nshttpurlresponse *response, Nserror *error,IDJSON) {9 Ten}];
The above is the setup request time-out for AFN.
For Sdwebimage,
Downloadimagewithurl in the sdwebimagedownloader.m file.
The specific:
1 -(ID<SDWebImageOperation>) downloadimagewithurl: (nsurl *) URL options: ( Sdwebimagedownloaderoptions) Options Progress: (void (^) (Nsuinteger,longlong)) Progressblock completed: (void (^) (UIImage *,nsdata *, Nserror *, BOOL)) Completedblock function.
The main:
1 -(ID) Initwithurl: (Nsurl *) URL CachePolicy: (nsurlrequestcachepolicy) CachePolicy timeOutInterval :(nstimeinterval) timeOutInterval; timeOutInterval in the
Decided.
If the request times out, we can cancel the AFN request, which is implemented by the following code:
1 //1. Create a AFN manager2Afhttprequestoperationmanager *mange =[Afhttprequestoperationmanager manager];3 4 //2. Sending the request5[Mange get:url Parameters:paramssuccess:^ (Afhttprequestoperation *operation,IDresponseobject) {6 //Request succeeded7Iwlog (@"Request succeeded");8 if(Success) {9 success (responseobject);Ten } One A} failure:^ (Afhttprequestoperation *operation, Nserror *error) { - -Iwlog (@"request failed"); the if(failure) { - failure (error); - } - }]; + - //3. Cancellation Request +[Mange.operationqueue cancelalloperations];
How to set the request time-out in iOS development