I have a quick question regarding memory management that I am not quite sure about. I currently have a locationManager delegate that calls locationManager:didUpdateToLocation:fromLocation
where it resolves the device location. My question is I am looking at adding ReverseGeoCoding after I have obtained the [newLocation coordinate]
but am unsure about doing the alloc here, as each time locationManager:didUpdateToLocation:fromLocation
gets called I will be alloc-ing a new MKReverseGeoCoder?
// LOCATION
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// GeoCoding:
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:[newLocati开发者_StackOverflow社区on coordinate]];
[geoCoder setDelegate:self];
[geoCoder start];
[self foundLocation];
}
Can anyone point me in the right direction with regards to this? I did try doing the alloc in application:didFinishLaunchingWithOptions: but then realised I did not have access to [newLocation coordinate].
EDIT_001:
DyingCactus, the reason that the delegate methods don't get called if you just add autorelease is I guess because the object is getting released, is that right? I think I was getting confused by thinking that MKReverseGeoCoder was going to stay around and service all my calls. It now seems that for each locationManager:didUpdateToLocation:fromLocation
a new instance is created with each call. This is what I have now, which feels a lot better:
// INTERFACE:
MKReverseGeocoder *reverseGeoCoding;
@property(nonatomic, retain) MKReverseGeocoder *reverseGeoCoding;
.
// IMPLEMENTATION:
self setReverseGeoCoding:[[[MKReverseGeocoder alloc] initWithCoordinate:location] autorelease]];
[reverseGeoCoding setDelegate:self];
[reverseGeoCoding start];
many thanks
gary
Adding an autorelease there removes the memory issue but then the delegate methods will not get called.
To solve both issues, declare the reverse geocoder at the class level. For an example, see the sample app CurrentAddress.
Also, if the foundLocation method is what you want to call when the reverse geocoding is done, call it in the reverse geocoder's didFindPlacemark method and not right after calling start.
精彩评论