与应用程序异步更新地图上的注释

Updating annotations on the map asynchronous with the app

我正在尝试在地图上更新注释时添加 activity 指示器。但它似乎不是这样工作的。一旦更新过程开始,应用程序屏幕就会禁用并冻结,所以这就是可能 activity 指示器不可见的原因。

我的问题是:是否可以与应用程序异步更新地图上的注释,以便 activity 指示器可见。

我认为如果超过 2 秒,用户看不到指示器真的很烦人。

解法:

self.activityIndicator.startAnimating()
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), {
                self.showPins(self.initialLat, locationLong: self.initialLong)
            })
self.activityIndicator.stopAnimating()

您必须确保您正在做这些事情:

1。一次加载所有注释

Apple 在 MKMapView Class Reference 中建议应立即加载所有注释(pin):

When configuring your map interface, you should add all of your annotation objects right away. The map view uses the coordinate data in each annotation object to determine when the corresponding annotation view needs to appear onscreen. [...] Because annotation views are needed only when they are onscreen, the MKMapView class provides a mechanism for queueing annotation views that are not in use. Annotation views with a reuse identifier can be detached and queued internally by the map view when they move offscreen. This feature improves memory use by keeping only a small number of annotation views in memory at once and by recycling the views you do have. It also improves scrolling performance by alleviating the need to create new views while the map is scrolling.


2。重用您的注释视图

您必须确保通过在 viewForAnnotation 方法中使用 dequeueReusableAnnotationViewWithIdentifier 正确地重用现有注释视图。


3。在后台线程上做重载

如果您不能立即获取/加载注释,您可以使用 Grand Central Dispatch 让 'heavy' 方法在后台线程上 运行 这样 UI不阻止。注意:从该方法(在 bg 线程上)对 UI 的任何更改都需要在主 UI 线程上显式发生。

关于背景,看看this Whosebug answer:

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
    print("This is run on the background queue")

   dispatch_async(dispatch_get_main_queue(), { () -> Void in
        print("This is run on the main queue, after the previous code in outer block")
    })
})

或者,使用这个“Async”库来获取一些语法糖。