Objective C - How to wait for the asynchronous call to finish -
i new developing in ios. have searched , tried several ways program not want wait asynchronous call finish.
when debugging, function checkforhost first returns -1 retval. causes method calling function continue. second later program gets function checkforhost setting correct value retval. tried nscondition no luck either...
can tell me doing wrong or should differently? many help!
the code below:
-(int)checkforhost { internetactive = -1; hostactive = -1; dispatch_queue_t myqueue = dispatch_queue_create("my queue", null); __block int retval = -1; dispatch_async(myqueue, ^{ [self hostinit]; dispatch_async(dispatch_get_main_queue(), ^{ [internetreachable stopnotifier]; [hostreachable stopnotifier]; [[nsnotificationcenter defaultcenter] removeobserver:self]; if (internetactive == 0) { retval = 0; } else if (hostactive == 0) { retval = 1; } else retval = 2; }); }); return retval; } -(void)hostinit { // check internet connection [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(checknetworkstatus:) name:kreachabilitychangednotification object:nil]; internetreachable = [reachability reachabilityforinternetconnection]; [internetreachable startnotifier]; // check if pathway random host exists hostreachable = [reachability reachabilitywithhostname:@"www.apple.com"]; [hostreachable startnotifier]; // patiently wait notification } -(void)checknetworkstatus:(nsnotification *)notice { // called after network status changes networkstatus internetstatus = [internetreachable currentreachabilitystatus]; switch (internetstatus) { case notreachable: { nslog(@"the internet down."); internetactive = 0; break; } case reachableviawifi: { nslog(@"the internet working via wifi."); internetactive = 1; break; } case reachableviawwan: { nslog(@"the internet working via wwan."); internetactive = 1; break; } } networkstatus hoststatus = [hostreachable currentreachabilitystatus]; switch (hoststatus) { case notreachable: { nslog(@"a gateway host server down."); hostactive = 0; break; } case reachableviawifi: { nslog(@"a gateway host server working via wifi."); hostactive = 1; break; } case reachableviawwan: { nslog(@"a gateway host server working via wwan."); hostactive = 1; break; } } }
i use blocks, they're easy , reliable
here example of function block :
- (void)checkforhostwithblock:(void (^)(int myreturnedint))completion{ //our function stuff here, i'll copy paste of code internetactive = -1; hostactive = -1; dispatch_queue_t myqueue = dispatch_queue_create("my queue", null); __block int retval = -1; dispatch_async(myqueue, ^{ [self hostinit]; dispatch_async(dispatch_get_main_queue(), ^{ [internetreachable stopnotifier]; [hostreachable stopnotifier]; [[nsnotificationcenter defaultcenter] removeobserver:self]; if (internetactive == 0) { retval = 0; } else if (hostactive == 0) { retval = 1; } else retval = 2; if (completion){ completion(retval); } }); }); }
there go.
you call method other method, , can use auto-complete make sure write down properly.
[self checkforhostwithblock:^(int myreturnint){ //executed code when block called in checkforhost: nslog(@"here int : %i !!", myreturnint); }];
note block parameter can nil need make sure check in method then.
Comments
Post a Comment