MKAnnotation not performing when compiled with XCode 10/11 but Xcode 9

Hello,


when I add some thousands simple annotations to a MKMapView, and complie & run the code with XCode 9 everything works as expected: smooth and well performing without any delays (at least with 5000 annotations on a MacBook Pro Late 2016).


However same code executed on XCode 10 or XCode 11 is no longer smooth until all annotations were loaded once. Which takes after some scrolling on the map 30 to 60 seconds.



My actual applicaiton requires many annotations which is no problem when compiling with XCode 9.


Is there a chance to get this also smooth after compiled with current XCode?

Or is this some general MapKit change under the hood?


And hep is appreciated.


Here my code:

import Cocoa
import MapKit


class ViewController: NSViewController {


    @IBOutlet weak var myMapView: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()


        var alle = [MKPointAnnotation]()
        
        
        for _ in 1...3000 {


            let randomx = Double(arc4random()) / 0xFFFFFFFF //value between 0 ... 1
            let randomy = Double(arc4random()) / 0xFFFFFFFF //value between 0 ... 1
            
            let lat = randomx * 100.0 - 50.0 // Value between -50.0 ... 50.0
            let lon = randomy * 100.0 - 50.0 // Value between -50.0 ... 50.0
            
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
            
            alle.append(annotation)
            
        }
        
        print ("Array contains now \(alle.count) annotations")
        
        myMapView.addAnnotations(alle)


    }


    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation is MKPointAnnotation else { return nil }
        
        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        
        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
        } else {
            annotationView!.annotation = annotation
        }
        
        return annotationView
    }
    
}

Replies

Generally, your first course of action in situations like this is to profile so you understand exacty where the performace hit is coming from, and what the difference is between the two versions.


I did try running your code with the different Xcode versions, and didn't see an obvious difference in performance. However, I may be misunderstanding how you are triggering what you are seeing. A video demonstrating the difference could help clarify how you reproduce this issue.

Thanks for your reply,


Here the example project I run on XCode 10 and XCode 9.4.1

https://www.xn.cx/tmptmp/Test-project.zip


Here are the videos, I can clean and run as often as I like the immense performance difference remains always:



XCode 9.4.1 https://www.xn.cx/tmptmp/XCode-9.mp4

  • After the debug message "Array contains now 3000 annotations" appears, the application is immediatly visible and scrolling/zooming works smooth without any delays or issues.
  • CPU is more or less ideling as long as I do not zoom/scroll


Xcode 10 (or 11) https://www.xn.cx/tmptmp/XCode-10.mp4

  • After the debug message "Array contains now 3000 annotations" appears, it takes long before the application window apperars.
  • CPU alsmost all the time 100% when trying to scroll/zoom
  • Memory increases higher than in XCode 9 and much slowler
  • Once the app window is visible it looks in the video like I do nothing, but I had the colored spinning mouse pointer while trying to scroll/zoom on the map, and the application freezes for several moments
  • At the end after each pin was on the screen once (after zooming out) it performs for futher zooming/scrolling as in the other case.


Does this help you to reproduce the problem?

The videos are useful so that I know what you're seeing, and how. Unfortunately, I still can't reproduce this with your project. I have similar launch times, CPU usage, and memory usage from builds with Xcode 9 and Xcode 10.

One interesting note, your build with Xcode 10 is picking up the light colored map, but you should be getting the dark map to match your macOS dark mode setting. Did you set some configuration for this?


Did you profile the Xcode 10 build to see how the time is being spent?