MKMapView dealloc'ed when calling addOverlay

匆匆过客 提交于 2019-12-12 13:14:09

问题


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:

  1. mapView & mapView.delegate is set to nil in - (void) dealloc and in - (void)viewDidDisappear
  2. BOOL finishedLoading that is set to FALSE when the view loads and set to TRUE when the map view delegate method - (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered is called.
  3. - (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

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