MKMapView uses lot of memory and keeps getting crash

75 views Asked by At

I am using Apple Maps to display approximately 2000 annotations and 50 polylines on the mapView. I have also implemented annotation clustering. However, my application is crashing due to high memory usage, and I occasionally receive an error message saying "abort_with_payload". I need assistance in resolving this issue to ensure smooth operation of my application.

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    guard let customPolyline = overlay as? CustomPolyline else {
        return overlay as? MKOverlayRenderer ?? MKOverlayRenderer()
    }
        
    let polylineRenderer = CustomPolylineRenderer(overlay: overlay)
        
    if let inspectedLineArray = self.searchInspectionList?.data {
        for inspectionLine in inspectedLineArray {
            guard let line = inspectionLine.line, customPolyline.lineid == line.id, let property = line.properties else {
                continue
            }
            polylineRenderer.strokeColor = UIColor(hexString: String(format: "%@ff", property.stroke ?? ""))
            polylineRenderer.lineWidth = CGFloat(property.strokeWidth ?? 0 * kLineWidth)
        }
    }
        
    return polylineRenderer
}
    
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let zoomWidth = mapView.visibleMapRect.size.width
    let zoomLevel = Int(log2(zoomWidth))
    self.currentZoomLevel = zoomLevel
}
    
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var annotationView: MKAnnotationView?
        
    switch annotation {
    case let nameAnnotation as NameAnnotation:
        annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: NameAnnotationView.ReuseID) ?? NameAnnotationView(annotation: nameAnnotation, reuseIdentifier: NameAnnotationView.ReuseID)
        annotationView?.image = Helper.generateImageWithText(text: nameAnnotation.title ?? "")
        annotationView?.clusteringIdentifier = kNameAnnotationClusterID
            
    case let customAnnotation as CustomAnnotation:
        if shouldCluster {
            annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CustomAnnotationView.ReuseID) as? CustomAnnotationView ?? CustomAnnotationView(annotation: customAnnotation, reuseIdentifiers: CustomAnnotationView.ReuseID)
            annotationView?.clusteringIdentifier = kCustomClusteringID
        } else {
            annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CustomAnnotationView.ReuseID) as? CustomAnnotationView ?? CustomAnnotationView(annotation: customAnnotation, reuseIdentifiers: CustomAnnotationView.ReuseID)
            annotationView?.clusteringIdentifier = nil
        }
            
        self.accessoryViewForCustomAnnotation(mapView, forAnnotationView: annotationView, annotation: customAnnotation)
        annotationView?.centerOffset = CGPoint(x: 0, y: -CGFloat((annotationView?.image?.size.height)! / 2))
            
        annotationView?.subviews.forEach { $0.removeFromSuperview() }
            
        let textLabel: UILabel?
            
        switch EntityType(rawValue: customAnnotation.entityTypes) {
        case .structures:
            textLabel = UILabel(frame: CGRect(x: -15, y: -20, width: 45, height: 20))
            if let name = (customAnnotation.title) {
                textLabel?.text = (customAnnotation.haveImage == true) ? String(format: "  %@*", name) : String(format: "  %@", name)
            }
            if let size = textLabel?.sizeThatFits(CGSize(width: Int(1000), height: 20)) {
                textLabel?.frame = CGRect(x: -(size.width)+(size.width / 2)+20, y: -20, width: size.width, height: 20)
            }
        case .forestryPin:
            textLabel = UILabel(frame: CGRect(x: -20, y: -20, width: 45, height: 20))
                                
            if let name = (customAnnotation.title) {
                textLabel?.text = (customAnnotation.haveImage == true) ? String(format: "  %@*", name) : String(format: "  %@", name)
            }
            if let size = textLabel?.sizeThatFits(CGSize(width: Int(1000), height: 20)) {
                textLabel?.frame = CGRect(x: -(size.width)+(size.width / 2)+20, y: -20, width: size.width, height: 20)
            }
        case .stations:
            textLabel = UILabel(frame: CGRect(x: -20, y: -20, width: 45, height: 20))
            if let name = (customAnnotation.title) {
                textLabel?.text = (customAnnotation.haveImage == true) ? String(format: "  %@*", name) : String(format: "  %@", name)
            }
            if let size = textLabel?.sizeThatFits(CGSize(width: Int(1000), height: 20)) {
                textLabel?.frame = CGRect(x: -20, y: -20, width: size.width, height: 20)
            }
        default:
            textLabel = nil
        }
            
        textLabel?.font = UIFont.systemFont(ofSize: 12)
        textLabel?.layer.borderColor = UIColor.white.cgColor
        textLabel?.layer.borderWidth = 2
        textLabel?.backgroundColor = UIColor.customMapRGB()
            
        if let textLabel = textLabel {
            annotationView?.addSubview(textLabel)
        }
            
    case let clusterAnnotation as MKClusterAnnotation:
        if let _ = clusterAnnotation.memberAnnotations.first as? CustomAnnotation {
            annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CustomClusterView.ReuseID) as? CustomClusterView ?? CustomClusterView(annotation: clusterAnnotation, reuseIdentifier: CustomClusterView.ReuseID)
        } else {
            annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: ClusterAnnotationView.ReuseID) ?? ClusterAnnotationView(annotation: clusterAnnotation, reuseIdentifier: ClusterAnnotationView.ReuseID)
        }
            
    case let userLocationAnnotation as MKUserLocation:
        annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") ?? MKAnnotationView(annotation: userLocationAnnotation, reuseIdentifier: "")
        annotationView?.image = #imageLiteral(resourceName: "current_location")
        annotationView?.canShowCallout = true
        annotationView?.detailCalloutAccessoryView = Helper.addLatLongInuserLocationCallOutView(userLocation: userLocationAnnotation)
            
    default:
        break
    }
        
    return annotationView
}
0

There are 0 answers