7

I'm developing an app to display about 200 GMSMarkers on GMSMapView I 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?

johnnyman
  • 129
  • 2
  • 11

2 Answers2

6

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
            }
        }
    }
}
ztan
  • 6,861
  • 2
  • 24
  • 44
  • 1
    Thanks for you reply, but how come it works (method 2) on a real device without an error? – johnnyman May 13 '15 at 15:56
  • Google Maps iOA SDK requires that all drawing events be done on the main thread, even the `let placeMarker = PlaceMarker(coordinate: location.coordinate)` has to be called inside the main thread. – ztan May 13 '15 at 15:57
  • Thank you very much I understand that "Google Maps iOA SDK REQUIRES that all drawing events be done on the main thread" but my questions is: Why is there no any error occur when I use method 2 on a real device. btw do you have any good marker cluster tutorial on swift can recommend? – johnnyman May 13 '15 at 16:17
  • Its probably a bug, you should do all your setup inside the main UI thread. http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift is a good Google Maps iOS SDK tutorial for Swift, but it doesnt talk about cluster. – ztan May 13 '15 at 16:30
  • If you found my answer solved your problem, would you mind marking it as accepted? @johnnyman – ztan May 14 '15 at 20:04
  • I appreciate your efforts, but I'm still looking for a better answer to solve this problem. Is there any other way to do this? If there is no better answer, I will check your answer in a week – johnnyman May 15 '15 at 10:43
3

Swift 3.0

dispatch_async(dispatch_get_main_queue()) 

has been renamed to

DispatchQueue.main.async 
nagam11
  • 133
  • 1
  • 9