Warning in Custom Map Annotations iPhone

谁说我不能喝 提交于 2019-12-17 16:29:23

问题


I am using a custom map annotation class for map view in iPhone. Whenever I pop my map view from navigation bar stack I usually see some warnings in console.

MapAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

I am not using KVO in my code hence not able to understand why I am receiving these warnings


回答1:


Latitude and Longitude have differing bounds:

  • (-90, 90) for Lat
  • (-180, 180) for Long

Passing a value outside of those bounds will cause the custom class to be deallocated and therefore giving you the error that you're receiving. Make sure that you are passing the correct values for both Latitude and Longitude.

It would be really nice if Apple passed a bounding error for this instead of an early release error. That would've saved me roughly 5 hours worth of time




回答2:


I was getting the same error as you:

An instance 0xa975400 of class xxxxxx was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0xa980eb0> (
<NSKeyValueObservance 0xa980e70: Observer: 0xa94f8a0, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0xa980ef0>

As pointed here it was caused because I was adding a MKAnnotation with an invalid coordinate to a MKMapView.

My solution was to create a function to check the coordinate valid.

Place+MKAnnotation.m

I created a category of my Place class and

#import "Place.h"
#import <MapKit/MapKit.h>

@interface Place (MKAnnotation) <MKAnnotation>
- (BOOL)coordinateIsValid;
...
@end

Place+MKAnnotation.m

@implementation Place (MKAnnotation)

- (BOOL)coordinateIsValid
{
    return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));

}
...
@end

I only add the annotation in my ViewController if the coordinate is valid.

if([p coordinateIsValid]) {
    [self.mapView addAnnotation:p];
} 



回答3:


Fixed it. I was using a wrong pair of latitude and longitude in annotations, I have changed the same and now everything seems to be perfect and warning has been disappeared as well.




回答4:


Are you autoreleasing your annotation before adding it to the MapView?

If so, try just allocating it, add it to the MapView, then release it.



来源:https://stackoverflow.com/questions/5872547/warning-in-custom-map-annotations-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!