ios - Make periodic server requests with NSURLSessionDataTask and dispatch_source timer -


need , explanation, because i'm stuck in question. need make this: 1) make 1 request server, response , want make request every 7 seconds(example). response. if satisfy several conditions -> stop timer , stuff.

main problem timer never stops, despite fact in response right. assume use gcd incorrectly. because in debug code behaves strange.

what have done:

this request function(it became after read 50 links how similar things)

-(void)makerequestwithurl:(nsstring*)urlstring andparams:(nsstring*)params andcompletionhandler:(void(^)(nsdictionary *responsedata, nserror *error))completionhnadler{  nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration]; nsurl *url = [nsurl urlwithstring:urlstring]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url                                                        cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; request.httpmethod = @"post"; request.httpbody = [params datausingencoding:nsutf8stringencoding];  nsurlsessiondatatask *postdatatask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) {      if (completionhnadler) {         if (error) {             dispatch_async(dispatch_get_main_queue(), ^{                 completionhnadler(nil, error);             });         } else {             nserror *parseerror;             json = [nsjsonserialization jsonobjectwithdata:data                                                    options:nsjsonreadingmutablecontainers                                                      error:&parseerror];             dispatch_async(dispatch_get_main_queue(), ^{                 completionhnadler(json, parseerror);             });         }     }  }];  [postdatatask resume]; } 

i create timer this:

dispatch_source_t createdispatchtimer(uint64_t interval,                                   uint64_t leeway,                                   dispatch_queue_t queue ,                                   dispatch_block_t block) { dispatch_source_t timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, queue); if (timer) {      // use dispatch_time instead of dispatch_walltime if interval small     dispatch_source_set_timer(timer, dispatch_walltime(null, 0), interval, leeway);     dispatch_source_set_event_handler(timer, block);     dispatch_resume(timer); } return timer; } 

and called this:

-(void)checkforpassenger {  timersource = createdispatchtimer(7ull * nsec_per_sec, 1ull * nsec_per_sec, dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{      if([self getnotificationsrequest] == yes) {         dispatch_source_cancel(timersource);     } else {         nslog(@"go on timer");     }     nslog(@"driver checked passenger!");  }); } 

this code of periodic response:

-(bool)getnotificationsrequest {  nsstring *urlstring = @"http://primetime.by/temproad/do"; nsstring *params = [nsstring stringwithformat:@"event={\"type\": \"in.getnotifications\"}&session_id=%@",session_id];   [self makerequestwithurl:urlstring andparams:params andcompletionhandler:^(nsdictionary *responsedata, nserror *error) {     if ([[responsedata objectforkey:@"rc"] intvalue] == 0) {           nsarray *temp_notifications = [responsedata objectforkey:@"notifications"];         if (temp_notifications.count != 0) {             notification = [[notification alloc] initwithnotification:[[responsedata objectforkey:@"notifications"] objectatindex:0]];         }     } }];  if (notification) {     return yes; } else {     return no; } } 

and in main request:

[self makerequestwithurl:urlstring andparams:params andcompletionhandler:^(nsdictionary *responsedata, nserror *error) {      if ([[responsedata objectforkey:@"rc"] intvalue] == 0) {         route = [[route alloc] initwithdata:responsedata];         dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{             [self checkforpassenger];         });      } }]; nslog(@"bye"); 

maybe explanation bad can answer question. thx


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -