Resolved Block help

Discussion in 'iOS Programming' started by larswik, Jan 11, 2014.

  1. larswik, Jan 11, 2014
    Last edited: Jan 11, 2014

    larswik macrumors 68000

    Sep 8, 2006
    EDIT: Disregard. It was an asynchronous operation that ran in the background. The result is returned from the Block just fine. I was NSLog'ing the results before the data was returned which is why I had the problem.

    I spent this morning using the CLLocation to try and get a zip code for the area a phone is in. The method works and I can see the zip code when using breakpoints. But for the life of me I can't figure out how to get data out of the block when it finishes? The block runs asynchronous, so off the main thread.

    I am trying to get the returned results out of the block to use else where. I first tried a global ivar but the results were NULL.

    The reverseGeocodeLocation: Method returns void so I have no idea how to return this data. I don't really understand blocks so any help would be appreciated in retrieving the data.

    - (void)reverseGeocodeLocation:(CLLocation *)location{
        CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
        if (reverseGeocoder) {
            [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
                CLPlacemark* placemark = [placemarks firstObject];
                if (placemark) {
                    //Using blocks, get zip code
                    NSString *zip = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
  2. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    Blocks inherit the scope in which they are defined. Thus they have access to instance variable from the object in who's instance method they are defined. Simply copy the value into an instance variable.
  3. larswik thread starter macrumors 68000

    Sep 8, 2006
    Cross post. I mixed up synchronous and asynchronous. The code was not waiting for data from the server. So when I printed out my NSLog the value I expected was not there because it had not been returned yet.

    Trying to figure it out I had the data assigned to a UILabel. Then after a couple seconds the UILabel changed to the correct zip code. I then realized the data takes longer to return.

    Was just staring at the code to long :)


Share This Page