xcode - Set NSData dataWithContentsOfURL timeout -
so have method in app returns bool if , update available apps content
- (bool)isupdateavailable{ nsdata *dataresponse=[nsdata datawithcontentsofurl:[nsurl urlwithstring:@"url returns json object"] ]; if(dataresponse!=nil){ nserror *error; dicupdates = [nsjsonserialization jsonobjectwithdata:dataresponse options:nsjsonreadingmutablecontainers error:&error]; } if(dicupdates.count > 0) isupdateavailable = yes; else isupdateavailable = no; return isupdateavailable; }
i need synchronous request this, cause next view controller dependent on server response. takes long time server respond or internet slow, need set time out prevent app 'being frozen'.
i used nsurlconnection accomplish task, has been deprecated.
also, tried using nsurlsession, (been using download updates in background thread), can figure out if can used synchronous request.
any idea how deal this? need synchronous method returns bool
. best regards.
we have use nsurlrequest in nsurlsession set timeout interval. check below code:
- (bool)isupdateavailable{ nsurlsession *session = [nsurlsession sharedsession]; [[session datataskwithrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:@"url returns json object"] cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:4]//timeout completionhandler:^(nsdata *dataresponse, nsurlresponse *response, nserror *error) { // handle response if(dataresponse!=nil){ nserror *error; dicupdates = [nsjsonserialization jsonobjectwithdata:dataresponse options:nsjsonreadingmutablecontainers error:&error]; } if(dicupdates.count > 0) isupdateavailable = yes; else isupdateavailable = no; return isupdateavailable; }] resume]; }
Comments
Post a Comment