How to set the request time-out in iOS development

Source: Internet
Author: User


1
NSString *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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.