iOS Block help

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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.

Code:
- (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];
                
            }
        }];
        
    }
}
 
Last edited:

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.

Code:
- (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];
                
            }
        }];
        
    }
}
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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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 :)

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.