Saturday, 24 August 2013

[SOLVED] Why is my code executing out of order?


hi guys,

sorry barrage of questions lately.

have code:

code:
 -(id)initwithcoordinate:(cllocationcoordinate2d)c title:(nsstring *)t location:(cllocation *)l {     self = [super init];          if(self)     {                  coordinate = c;                  //create date          nsstring *date = [nsdateformatter localizedstringfromdate: [nsdate date]                                                         datestyle:nsdateformattershortstyle                                                          timestyle:nsdateformattershortstyle];                  //create reversegeocode         if(!geocoder)         {             geocoder = [[clgeocoder alloc] init];         }                  [geocoder reversegeocodelocation:l                        completionhandler:^(nsarray *placemarks, nserror *error) {                                                        for(clplacemark *p in placemarks)                            {                                                                                                [self setlocality:[p locality]];                            [b]    nslog(@"inside: %@", [self locality]); //this executes second[/b]                                [self setadministrativearea:[p administrativearea]];                                                            }                                                    }];                   [b]        nslog(@"outside: %@", [self locality]); //this executes first!!![/b]                  [self settitle:t];         [self setsubtitle:date];              }          return self; }
its test try , figure out why [self locality] being assigned nil on "outside" nslog statement being assigned value on "inside" nslog statement.

here program output:

code:
 [b]2012-02-07 05:36:22.740 whereami[2393:11603] outside: (null) 2012-02-07 05:36:23.000 whereami[2393:11603] inside: san francisco[/b]
as can see reason "outside" executing before "inside" , preventing me finishing exercise.

looked @ documentation, doesn't seem anything. there property called "bool isgeocoding" when not believe think because when wrap around nslog(@"outside") line line never executes, suggesting geoencoder never finishes encoding value.

trying make in "reversegeocodelocation" block execute before "nslog(@"outside") line , having trouble finding way it. suggestions appreciated.
 

the inside: log statement in block. block chunk of code , data can pass around object. passing reversegeocodelocation:completionhandler: method completionhandler. whilst i've not read documentation seems clear method asynchronous , completion handler called @ point in future when reverse geocode lookup has completed. results expect see.

edit: if want stuff after reverse lookups started run after complete i'd suggest moving them method call within block tracks how many outstanding lookups running , when counter reaches zero.
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment