问题描述
我正在开发一个应用程序,以在GMSMapView
上显示大约200个GMSMarkers。我尝试了2种显示标记的方法。 Method1虽然有点慢,但没有发生错误,但是,Method2可以在真实的设备上顺利运行,但是当我在iOS模拟器上测试时,我得到了GMSThreadException。
以下是问题:
1.继续使用method2可以吗?
2.如果继续使用method2是不好的,有什么好的建议可以减少整个过程的加载时间?
在locationsArrayFromSomeWhere中的位置{
let placeMarker = PlaceMarker(coordinate:location.coordinate)
。
。//简单安装
。
placeMarker.map = self.mapView
}
}
func Method2(){
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value),0 )){
在locationsArrayFromSomeWhere中的位置{
let placeMarker = PlaceMarker(coordinate:location.coordinate)
。
。//简单安装
。
dispatch_async(dispatch_get_main_queue()){
placeMarker.map = self.mapView
}
}
}
}
在此输入代码
任何帮助表示赞赏Orz
UPDATE1
正如@ztan在下面回答的,我必须在主线程中完成所有这些工作,有没有比这更好的解决方案?
因此,对于第二个方法,你必须把所有的制造商设置代码放在 dispatch_get_main_queue()
闭包中。
$ b $ pre $
func Method2(){
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value),0) ){
位置位置ArrayFromSomeWhere {
dispatch_async(dispatch_get_main_queue()){
让placeMarker = PlaceMarker(坐标:location.coordinate)
。
。//简单安装
。
placeMarker.map = self.mapView
}
}
}
}
I'm developing an app to display about 200 GMSMarkers on GMSMapViewI tried 2 methods to display the markers. Method1 is a bit slow but no error occur, however, Method2 runs smoothly on a real device but I got GMSThreadException when I test it on iOS Simulator
Here are the questions: 1. Is it ok to keep using method2? 2. If it is not ok to keep using method2, any good suggestions to reduce the loading time for the whole process?
func Method1() {
for location in locationsArrayFromSomeWhere {
let placeMarker = PlaceMarker(coordinate: location.coordinate)
.
.//Simple Setup
.
placeMarker.map = self.mapView
}
}
func Method2() {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
for location in locationsArrayFromSomeWhere {
let placeMarker = PlaceMarker(coordinate: location.coordinate)
.
.//Simple Setup
.
dispatch_async(dispatch_get_main_queue()) {
placeMarker.map = self.mapView
}
}
}
}
enter code here
Any help is appreciated Orz
UPDATE1
As @ztan answered below, I have to do all this in the main thread, is there any better solution than this?
Google Maps iOA SDK requires that all drawing events be done on the main thread.
So, for your second method, you have to do put all your maker setup code inside the dispatch_get_main_queue()
closure.
So your method 2 will be:
func Method2() {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
for location in locationsArrayFromSomeWhere {
dispatch_async(dispatch_get_main_queue()) {
let placeMarker = PlaceMarker(coordinate: location.coordinate)
.
.//Simple Setup
.
placeMarker.map = self.mapView
}
}
}
}
这篇关于在iOS模拟器上显示GMSMarkers时发生GMSThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!