LocationManager 调度队列问题
LocationManager dispatch queue Issue
我构建的内容:
我用 CLLocationManagerDelegate
构建了一个简单的 GMSMapView
,它只跟踪用户的当前位置并在 GMSMapView
上更新。
问题:
当 CLLocationManagerDelegate
(GMSMapView
屏幕)直接打开时它工作得很好,但是当我尝试使用 segue
到达 GMSMapView
屏幕时它推送错误。
位置管理器 (0x145e5f9e0
) 是在主线程以外的线程上执行的调度队列上创建的。开发人员有责任确保在分配位置管理器对象的线程上有一个 运行 循环 运行ning。特别是,不支持在任意调度队列(不附加到主队列)中创建位置管理器,这将导致无法接收到回调。
您必须将查找位置的代码放入调度队列而不是主队列。
使用此代码
DispatchQueue.main.async
{
/*your code here*/
};
使用 Swift 3。所以问题在于变量 GMSMapView
和 CLLocationManager
的初始化。这就是它对我有用的方式:
我在 ViewController
class 中定义变量如下
private var locationManager: CLLocationManager!
private var googleMapView: GMSMapView!
在 viewDidLoad()
中,我在主线程中引入了 DispatchQueue
到 运行。
DispatchQueue.main.async {
//adding mapView
self.googleMapView = GMSMapView()
self.view.addSubview(self.googleMapView)
self.googleMapView.translatesAutoresizingMaskIntoConstraints = false
//auto layout constraints
self.googleMapView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
self.googleMapView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
self.googleMapView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
self.googleMapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
//location manager setup
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.startUpdatingHeading()
}
我构建的内容:
我用 CLLocationManagerDelegate
构建了一个简单的 GMSMapView
,它只跟踪用户的当前位置并在 GMSMapView
上更新。
问题:
当 CLLocationManagerDelegate
(GMSMapView
屏幕)直接打开时它工作得很好,但是当我尝试使用 segue
到达 GMSMapView
屏幕时它推送错误。
位置管理器 (0x145e5f9e0
) 是在主线程以外的线程上执行的调度队列上创建的。开发人员有责任确保在分配位置管理器对象的线程上有一个 运行 循环 运行ning。特别是,不支持在任意调度队列(不附加到主队列)中创建位置管理器,这将导致无法接收到回调。
您必须将查找位置的代码放入调度队列而不是主队列。
使用此代码
DispatchQueue.main.async
{
/*your code here*/
};
使用 Swift 3。所以问题在于变量 GMSMapView
和 CLLocationManager
的初始化。这就是它对我有用的方式:
我在 ViewController
class 中定义变量如下
private var locationManager: CLLocationManager!
private var googleMapView: GMSMapView!
在 viewDidLoad()
中,我在主线程中引入了 DispatchQueue
到 运行。
DispatchQueue.main.async {
//adding mapView
self.googleMapView = GMSMapView()
self.view.addSubview(self.googleMapView)
self.googleMapView.translatesAutoresizingMaskIntoConstraints = false
//auto layout constraints
self.googleMapView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
self.googleMapView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
self.googleMapView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
self.googleMapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
//location manager setup
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.startUpdatingHeading()
}