Greg Maletic
Greg Maletic

Reputation: 6337

Objective-C: Using a block as a completion handler

I'm using CLGeocoder, and I'm using a block as a completion handler. I'm unsure of the retain/release cycle for the instance of CLGeocoder that I create.

Here's the basic code:

CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:
    ^(NSArray* placemarks, NSError* error)
    {
        // process the placemarks...
        [geocoder autorelease];
    }
];

Is autoreleasing the geocoder as the last line of the block the recommended way to handle this? Any suggestions are appreciated!

Upvotes: 0

Views: 1491

Answers (1)

bendu
bendu

Reputation: 391

You can just release it (no need for auto release) Auto release is for when you are unsure when you will need to release an object (such as returning an object at the end of a method or for convenience methods)

In this case you are certain that you are done using the object so it may be released. Of course, autorelease works too, but lingers around in memory longer.

Upvotes: 1

Related Questions