2017-02-03 12 views
1

Alamofire를 사용하여 서버에서 데이터를 요청한 후이 데이터를 사용하여지도에 위치를 표시합니다. 지도를 이동하거나 확대/축소 할 때 API를 요청하고지도를 다시로드해야합니다.Alamofire와 Mapkit Swift 3으로 요청하십시오.

확대/축소 또는 패닝 할 때마다 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

+0

'CPU> 100 %'는 무엇을 의미합니까? –

+0

@NhatDinh 평생 디버깅에서 CPU> 100 % 및 높은 메모리. https://i.stack.imgur.com/tDpIo.png –

+0

힌트 : 프로파일 러 도구를 사용하여 코드에서 메모리 누수를 찾을 수 있습니다! –

답변

0

당신은 메인 스레드에 API 요청을하고, 노 노.하지

백그라운드 스레드에서 API 요청을 실행 한 다음 주 스레드에서지도를 업데이트해야합니다. 좋아요 :

 
DispatchQueue.global(qos: .userInitiated).async { 
    SearchWebservice.sharedInstance.getResultMap(currentSearchObject, success: { (result) in 

     //process the results 

     //make sure to do any UI/Map stuff back on the main thread 
     DispatchQueue.main.async(execute: { [unowned self] in 
      self.mapView.addAnnotation(marker) 
     }) 

    } 
}