本文介绍了分配mapView时,随机KVO块崩溃,仅在多次打开/关闭屏幕时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序多次打开和关闭时在地图屏幕上崩溃. (主要是第六次尝试)

App is crashing on a map screen when it open and close several times. (Mostly on 6th attempt)

GMSMapView

Class that inherits from GMSMapView

class AirportMapView: GMSMapView , AirportMapViewProtocol{

weak var airportMapViewModuleDelegate: AirportMapViewModuleProtocol?

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
 }
override init(frame: CGRect) {
    super.init(frame: frame)
 }
convenience init(from frame: CGRect, with cameraPosition: GMSCameraPosition) {
    self.init(frame: frame)
    self.camera = cameraPosition
 }
  func setCluster() {

    let algorithm = CustomGoogleMapsClusteringAlgorithm.init();
    mapIconGenerator = VJAirportIconGrayClusterGenerator.init()
    let renderer = VJGoogleMapsClusterRenderer(mapView: self,
                                               clusterIconGenerator: mapIconGenerator!)
    clusterManager = GMUClusterManager.init(map: self, algorithm: algorithm, renderer: renderer)
    clusterManager?.setDelegate(self, mapDelegate: self)

 }
}

在ViewController viewDidLoad中,我正在调用mapView的init方法

In ViewController viewDidLoad I am calling init method of mapView

self.mapView = [[AirportMapView alloc] initFrom:frame with:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.settings.compassButton = YES;
self.mapView.settings.zoomGestures = YES;
self.mapView.airportMapViewModuleDelegate = self;

崩溃的回溯记录和附加的控制台日志

观察:

    如果我删除addObserver代码应用未崩溃的
  • GMUClusterManager initWithMap方法
  • GMUClusterManager initWithMap method if I remove addObserver code app is not crashing

推荐答案

检查在Google-Maps-iOS-Utils 源代码中,事实证明GMSClusterManager类并未维护对其通过KVO观察到的GMSMapView的强引用.如果在dealloc中调用removeObserver:forKeyPath:方法之前曾经重新分配过GMSMapView对象,则可能导致崩溃.

After inspecting the Google-Maps-iOS-Utils source code, it turns out that the GMSClusterManager class did not maintain a strong reference to the GMSMapView that it observed via KVO. This could potentially cause crashes if the GMSMapView object ever deallocated before the removeObserver:forKeyPath: method could be called from dealloc.

Apple文档 ,应为通过KVO观察到的对象维护强引用:

Per Apple documentation, strong references should be maintained for objects observed via KVO:

请参见此提取请求(现已合并) )以获取更多详细信息.

See this pull request (which is now merged) for more details.

这篇关于分配mapView时,随机KVO块崩溃,仅在多次打开/关闭屏幕时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:35