【问题标题】:Request with Alamofire and Mapkit Swift 3使用 Alamofire 和 Mapkit Swift 3 请求
【发布时间】:2017-02-03 03:00:50
【问题描述】:

我正在使用 Alamofire 从服务器请求数据,然后使用这些数据在地图上显示位置。平移或缩放地图时,需要请求 API 并重新加载地图。

当前的问题是,无论何时进行缩放或平移,CPU > 100% 都会导致无法缩放和平移。

我的代码如下:

Alamofire 请求:

func getResultMap() {
    DispatchQueue.main.async( execute: {
        self.skipSearchRequest = true

        SearchWebservice.sharedInstance.getResultMap(currentSearchObject, success: { (result) in
            var clusters: [Cluster] = []
            var markers : [Marker] = []
            if self.mapLayerType == .ClusterOverview {
                for item in result.clusterOverview {
                    clusters.append(item)
                }
                self.setMapAnnotations(annotations: clusters)
            } else {
                for item in result.markers {
                    markers.append(item)
                }
                self.addMapAnnotations(annotations: markers)
            }
            self.skipSearchRequest = false
        }) { (error) in
            print(error)
        }
    })
}

添加/删除注释:

 func addMapAnnotations(annotations: [MKAnnotation]) {
    var annotationsToRemove :[MKAnnotation] = []
    var annotationsToAdd: [MKAnnotation] = annotations
    let annotationsTemp: [MKAnnotation] = annotations

    for item in mapView.annotations {
        if item.isKind(of: Cluster.self) {
            annotationsToRemove.append(item)
        } else if item.isKind(of: Marker.self) {
            for itemTemp in annotationsTemp {
                if itemTemp.coordinate.latitude == item.coordinate.longitude && itemTemp.coordinate.longitude == item.coordinate.longitude {
                    annotationsToAdd.remove(at: annotationsToAdd.index(where: {$0.coordinate.latitude == itemTemp.coordinate.latitude && $0.coordinate.longitude == itemTemp.coordinate.longitude})!)
                }
            }
        }
    }
    mapView.removeAnnotations(annotationsToRemove)
    mapView.addAnnotations(annotationsToAdd)
}

func setMapAnnotations(annotations: [MKAnnotation]) {
    self.mapView.removeAnnotations(self.mapView.annotations)
    var annotationsToAdd: [MKAnnotation] = []
    for item in annotations {
        if item.isKind(of: Cluster.self) {
            annotationsToAdd.append(item)
        } else if item.isKind(of: Marker.self) {
            annotationsToAdd.append(item)
        }
    }
    DispatchQueue.main.async {
         self.mapView.addAnnotations(annotationsToAdd)
    }
}

地图委托:

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if !skipSearchRequest {
            Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(COHFResultMapViewController.getResultMap), userInfo: nil, repeats: false)
    } else {
        skipSearchRequest = false
    }

}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation.isKind(of: Cluster.self) {
        let reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? ClusterView
        if clusterView == nil {
            clusterView = ClusterView(annotation: annotation, reuseIdentifier: reuseId)
        } else {
            clusterView?.annotation = annotation
        }
        return clusterView
    } else {
        let reuseId = "Pin"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

        if annotationView == nil {
            annotationView = AnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        } else {
            annotationView?.annotation = annotation
        }
        return annotationView
    }
}

如何提高在 Mapkit 中重新加载注解的性能和最佳实践?

Xcode console debugging output

【问题讨论】:

  • CPU > 100% 是什么意思?
  • @NhatDinh 在生命周期调试中,CPU > 100% 和高内存。 i.stack.imgur.com/tDpIo.png
  • 提示:您可以使用分析器工具来查找代码中的内存泄漏!

标签: ios swift3 mapkit alamofire mapkitannotation


【解决方案1】:

您正在主线程上执行 API 请求,这是不行的。

您需要在后台线程上运行 API 请求,然后在主线程上更新地图。像这样:

DispatchQueue.global(qos: .userInitiated).async { SearchWebservice.sharedInstance.getResultMap(currentSearchObject, success: { (result) in //处理结果 //确保在主线程上做任何 UI/Map 的东西 DispatchQueue.main.async(execute: { [unowned self] in self.mapView.addAnnotation(标记) }) } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多