问题
I'm having an issue with my mapView being dealloced when I call this in - (void)viewDidLoad
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
self.onlineOverlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
self.onlineOverlay.canReplaceMapContent = YES;
[_mapView addOverlay:self.onlineOverlay level:MKOverlayLevelAboveLabels];
The log is:(with zombie objects enabled)
*** -[VKRasterOverlayTileSource invalidateRect:level:]: message sent to deallocated instance 0xf1863e0
It only happens when I dismiss the view controller containing the map view too soon. If I wait a few seconds it doesn't crash. The view controller gets dismissed by pop'ing it from the navigation controller,
I've tried several options:
mapView
&mapView.delegate
is set tonil
in- (void) dealloc and in - (void)viewDidDisappear
BOOL finishedLoading
that is set toFALSE
when the view loads and set toTRUE
when the map view delegate method- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
is called.- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers
seems to be called before the method is done.
How can I check if the overlay has been fully added?
Or is it possible to cancel the overlay if the view controller gets dismissed?
EDIT:
The view controller is in a navigation controller stack and is presented and popped.
I'm not using any threads myself. From instruments I can see that there is another thread, so my guess is that addOverlay:
is creating a thread somewhere.
The mapView is a property of the viewController and is the delegate as well.
回答1:
Set a pointer to your mapView in the tileOverlay subclass. This way the mapView is still alive when the result block is called. You will have to manually add the mapView property in your tileOverlay subclass.
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
self.onlineOverlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
self.onlineOverlay.canReplaceMapContent = YES;
self.onlineOverlay.mapView = _mapView;
[_mapView addOverlay:self.onlineOverlay level:MKOverlayLevelAboveLabels];
来源:https://stackoverflow.com/questions/21957257/mkmapview-dealloced-when-calling-addoverlay