ios - How to call method only after location data were received from CLLocaitonManager delegate? -


i have longitude , latitude of shops in city. after determine distance between device/user location , shops, data show in table. default when app launches need call method sort shops distances , reload tableview. please suggest me how , (sequence of methods) call sorting method, because device/user location data not yet received , table sorted wrong. bad english, sorry

to determine location i'am using cllocationmanagerdelegate:

- (void)locationmanager:(cllocationmanager *)manager     didupdatetolocation:(cllocation *)newlocation            fromlocation:(cllocation *)oldlocation {     mynewlocation = newlocation;     [_tableview reloaddata]; } 

then (cclocationdistance)distancefromlocation: method in tableview:cellforindexpath

sorting method called in viewdidload:

- (nsarray*)getsortedarray:(nsarray*)shops bylocation:(cllocation*)userlocation{     nsarray *sortedarray;     sortedarray = [shops sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2){         shop *dict1 = (shop*)obj1;         shop *dict2 = (shop*)obj2;         cllocation *shoplocation = [[cllocation alloc]initwithlatitude:[[dict1 lat]doublevalue]                                                                  longitude:[[dict1 lon]doublevalue]];         cllocation *shoplocation2 = [[cllocation alloc]initwithlatitude:[[dict2 lat]doublevalue]                                                                  longitude:[[dict2 lon]doublevalue]];         cllocationdistance key1 = [userlocation distancefromlocation:shoplocation];         cllocationdistance key2 = [userlocation distancefromlocation:shoplocation2];         nsnumber *num1 = [nsnumber numberwithdouble:key1];         nsnumber *num2 = [nsnumber numberwithdouble:key2];         return [num1 compare:num2];     }];     return sortedarray; } 

you use block. blocks can used callbacks, defining code executed when task completes.

here example docs

- (ibaction)fetchremoteinformation:(id)sender { [self showprogressindicator];  xyzwebtask *task = ...  [task begintaskwithcallbackblock:^{     [self hideprogressindicator]; }]; } 

this example calls method display progress indicator, creates task , tells start. callback block specifies code executed once task completes; in case, calls method hide progress indicator.

in case, callback block contain code sorting, wait location data received.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -