ios - Swift 在另一个线程的 mapView 中显示注释

标签 ios iphone swift

此代码不向 mapView 添加注释。我在一个答案中看到,每次调用 addAnotation 时都会调用 mapView 函数,那么问题出在哪里?但是当我移动 map 时,它们会出现。

func addPlacesMarkers(location:CLLocation) {
    self.communication.getJsonData(self.getPubsNearByCreator(location)) { (finalData, error) -> Void in
        if error != nil {
            print(error)
        } else {
            if let row: NSArray = finalData {
                for var i = 0; i < row.count; i++ {
                    let lat = row[i]["lat"] as! String
                    let lng = row[i]["lng"] as! String
                    let title = row[i]["name"] as! String
                    let id = row[i]["id"] as! String

                    let point = CustomizedAnotation(id: Int(id)!, name: title)

                    point.title = title
                    point.coordinate.latitude = Double(lat)!
                    point.coordinate.longitude = Double(lng)!
                    let keyExists = self.places[Int(id)!] != nil
                    if keyExists == false {
                        self.places.updateValue(point, forKey: Int(id)!)
                    }
                }

                var finalPlaces :[MKPointAnnotation] = []

                for place in self.places.values {
                    finalPlaces.append(place)
                }

                self.mView.addAnnotations(finalPlaces)
                self.mView.showsPointsOfInterest = false
            }
        }
    }
}

最佳答案

您不能在与主线程不同的线程中修改 UI。 您应该将 UI 修改代码放在 dispatch_async block 中,如下所示:

dispatch_async(dispatch_get_main_queue()) {
    //Your code that modify the UI 
    self.mView.addAnnotations(finalPlaces)
}

关于ios - Swift 在另一个线程的 mapView 中显示注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548796/

相关文章:

ios - 在 iOS 7 上,如果最终包含在选项卡栏 Controller 中,则使用工具栏推送 Controller 会留下不可用空间的间隙

ios - 如何在 Alamofire 中将原始数据作为参数发布?

java - Linux 服务器上的 HTTP 直播

iphone - 为什么我的文件不移动到 "en.lproj",而是移动到 Xcode 中 "English.lproj"之后的新 "Make file Localizable"?

c# - 我可以使用 MonoTouch 将旧的 C# 桌面应用程序移植到 iPad 上吗?

swift - 无法使用 'String' 类型的参数列表调用 CGFloat 类型的初始值设定项

ios - 自定义导航栏后退按钮标题

ios - 为 iPad 实现自定义 Split View(OpenGL + 控件)

ios - 如何使用 Appium 在 iOS 移动自动化中隐藏键盘

ios - Swift 中的无限 Tableview