问题描述
应用程序多次打开和关闭时在地图屏幕上崩溃. (主要是第六次尝试)
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;
崩溃的回溯记录和附加的控制台日志
观察:
- 如果我删除
-
GMUClusterManager
initWithMap
方法
addObserver
代码应用未崩溃的GMUClusterManager
initWithMap
method if I removeaddObserver
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块崩溃,仅在多次打开/关闭屏幕时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!