GMSThreadException occurs when displaying GMSMarkers on iOS Simulator

I am developing an application to display about 200 GMSMarkers on a GMSMapView. I tried 2 methods for displaying markers. Method1 is a little slow, but no error occurs, however Method2 runs smoothly on a real device, but I got a GMSThreadException when I test it on iOS Simulator

Here are the questions: 1. Can I continue to use method2? 2. If you can’t continue to use method2, any good suggestions for reducing load times 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 by Orz

Update1

As @ztan answered below, I have to do all this in the main thread, is there a better solution than this?

+5
source share
2 answers

The Google Maps iOA SDK requires that all drawing events run in the main thread.

So, for your second method, you need to put all of your manufacturer’s installation 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 } } } } 
+5
source

Swift 3.0

 dispatch_async(dispatch_get_main_queue()) 

was renamed to

 DispatchQueue.main.async 
+1
source

All Articles