ios - Refer back to background queue with grand central dispatch/dispatch_async in Objective-C -


i trying run process in background on period of time ensure user can still navigate application whilst collect data. have discovered should utilise gcd this. however, i'm unsure how make arraybuild run in background queue well.

how add background queue? application works fine when reference dispatch_async removed know issue thread.

- (ibaction)buttonpressed:(id)sender {      dispatch_async(backgroundqueue, ^{          manager.delegate = self;         manager.desiredaccuracy = kcllocationaccuracybest;          /* removed testing         [manager startupdatinglocation];          */          // test          mytimer = [nstimer scheduledtimerwithtimeinterval:1.0                                                    target:self                                                  selector:@selector(arraybuild)                                                  userinfo:nil                                                   repeats:yes];          // initialise array         resultsarray = [[nsmutablearray alloc]init];          // end test      });  }  - (void)arraybuild {      loopcount++;      if (loopcount >= 11) {          // invalidate timer         [mytimer invalidate];         mytimer = nil;          // find average         nsnumber *avg = [resultsarray valueforkeypath:@"@avg.self"];          // change text of label average & log         self.signal.text = [nsstring stringwithformat:@"%@",avg];         nslog(@"%@",avg);        }else{          // declare signal strength         float signalstrength = ctgetsignalstrength();          // individual result & convert integer         nsstring *result = [nsstring stringwithformat:@"%f", signalstrength];         nsinteger resultint = [result integervalue];          // add object         [resultsarray addobject:[nsnumber numberwithfloat:resultint]];          // change text of label each second         self.signal.text = [nsstring stringwithformat:@"%d",loopcount];         nslog(@"%f",signalstrength);      }  } 

you cannot create timers in background queue that. nstimer needs run loop , background queues not have one. use gcd timer source if want timer fire on custom background queue (or worse, create own nsthread , start run loop on thread, , schedule nstimer on thread's run loop), it's going easier eliminate gcd code , start timer main run loop (i.e. don't dispatch creation of timer background queue).

likewise, should configure , start cllocationmanager location updates main thread, too. don't think causes problem have, it's unnecessary.

timers , location updates happen asynchronously , long appropriate handler runs reasonably quickly, don't worry trying schedule them on background queues. main thread fine.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -