i creating nsmutablerequest:
self.req = [nsmutableurlrequest requestwithurl:myurl cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:10.0]; the timeout set 10 seconds because don't want user wait long feedback. after create nsurlsessiondatatask:
nsurlsessiondatatask *task = [self.session datataskwithrequest:self.req completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nshttpurlresponse * httpresp = (nshttpurlresponse *)response; if (error) { // timeout } else if (httpresp.statuscode < 200 || httpresp.statuscode >= 300) { // handling error , giving feedback } else { nserror *serializationerror = nil; nsdictionary *jsondict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&serializationerror]; } [task resume]; } the problem server goes gateway timeout , takes lot of time. timeout error , give feedback user, following api calls fail in same way due timeout error. way stop kill app , start over. there should kill task or connection after timeout error? if don't set timeout , wait until receive error code server following calls work (but user waits lot!).
i tried cancel task:
nsurlsessiondatatask *task = [self.session datataskwithrequest:self.req completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nshttpurlresponse * httpresp = (nshttpurlresponse *)response; if (error) { // timeout [task cancel]; } ... [task resume]; }
i didnt see resume task started. need declare:
[task resume]; this line resumes task, if suspended.
try call nsurlsession follows:
[nsurlsession sharedsessison] instead of self.session and invalidate session by:
[[nsurlsession sharedsession]invalidateandcancel]; from apple's documentation:
when app no longer needs session, invalidate calling either invalidateandcancel (to cancel outstanding tasks) or finishtasksandinvalidate (to allow outstanding tasks finish before invalidating object).
- (void)invalidateandcancel once invalidated, references delegate , callback objects broken. session objects cannot reused.
to allow outstanding tasks run until completion, call finishtasksandinvalidate instead.
- (void)finishtasksandinvalidate this method returns without waiting tasks finish. once session invalidated, new tasks cannot created in session, existing tasks continue until completion. after last task finishes , session makes last delegate call, references delegate , callback objects broken. session objects cannot reused.
to cancel outstanding tasks, call invalidateandcancel instead.
Comments
Post a Comment