Message from debugger: Terminated due to memory issue

前端 未结 6 1599
小蘑菇
小蘑菇 2021-02-18 21:52

My app working with Geojson file. I use MapBox SDK to add MGLPolyline to map. But the problem is my file too large, so that the app crash and got the e

6条回答
  •  不思量自难忘°
    2021-02-18 22:33

    I had some problems to test your project with pods, so I've directly remove pods and use Mapbox framework directly from here.

    I've no problems for the first launches both in simulator and real iPad (my iPad 4 gen.) but after a while I've your same error, so I've correct this code with:

    DispatchQueue.main.async {
          // weaked reference to self to prevent retain cycle
          [weak self] in
          guard let strongSelf = self else { return } 
          strongSelf.mapboxView.addAnnotation(line)
    }
    

    because unowned it's not enough to prevent retain cycle. Now seems it works well.

    Hope it helps.

    P.S. (I've used the latest available Mapbox v3.3.6)


    Update (after comments):

    So, first of all I make all my test with Mapbox framework inserted as "embedded framework".

    I've make some corrections to your github project only to ViewController.swift to avoid retain cycles. P.S. I remove comments lines to make easy reading:

    func drawPolyline() {
            DispatchQueue.global(qos: .background).async {
                [weak self] in
                guard let strongSelf = self else { return }
                let jsonPath = Bundle.main.path(forResource: "KMLMAPNew", ofType: "json")
                let jsonData = NSData(contentsOfFile: jsonPath!)
                do {
                    guard let jsonDict = try JSONSerialization.jsonObject(with: jsonData! as Data, options: []) as? Dictionary, let features = jsonDict["features"] as? Array else{return}
                    for feature in features {
                        guard let feature = feature as? Dictionary, let geometry = feature["geometry"] as? Dictionary else{ continue }
                        if geometry["type"] as? String == "LineString" {
                            var coordinates: [CLLocationCoordinate2D] = []
                            if let locations = geometry["coordinates"] as? Array {
                                for location in locations {
                                    if let location = location as? Array{
                                        let coordinate = CLLocationCoordinate2DMake(location[1].doubleValue, location[0].doubleValue)
                                        coordinates.append(coordinate)
                                    }
                                }
                            }
                            let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
                            line.title = "Crema to Council Crest"
                            print(feature) // Added this line just for debug to see the flow..
                            DispatchQueue.main.async {
                                strongSelf.mapboxView.addAnnotation(line)
                            }
                        }
                    }
                }
                catch
                {
                    print("GeoJSON parsing failed")
                }
            }
        }
    
    func newWay(){
            DispatchQueue.global(qos: .background).async {
                [weak self] in
                guard let strongSelf = self else { return }
                let jsonPath = Bundle.main.path(forResource: "KMLMAPNew", ofType: "json")
                let jsonData = NSData(contentsOfFile: jsonPath!)
                do {
                    if let jsonDict = try JSONSerialization.jsonObject(with: jsonData! as Data, options: []) as? Dictionary {
                        if let features = jsonDict["features"] as? Array {
                            let chunks = stride(from: 0, to: features.count, by: 2).map {
                                Array(features[$0..])
                            }
                        }
                    }
                }
                catch
                {
                    print("GeoJSON parsing failed")
                }
            }
        }
    
    func drawSmallListObj(list: [Dictionary]){
            for obj in list{
                if let feature = obj as? Dictionary {
                    if let geometry = feature["geometry"] as? Dictionary {
                        if geometry["type"] as? String == "LineString" {
                            var coordinates: [CLLocationCoordinate2D] = []
                            if let locations = geometry["coordinates"] as? Array {
                                for location in locations {
                                    if let location = location as? Array{
                                        let coordinate = CLLocationCoordinate2DMake(location[1].doubleValue, location[0].doubleValue)
                                        coordinates.append(coordinate)
                                    }
                                }
                            }
                            let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
                            line.title = "Crema to Council Crest"
                            DispatchQueue.main.async {
                                [weak self] in
                                guard let strongSelf = self else { return }
                                strongSelf.mapboxView.addAnnotation(line)
                            }
                        }
                    }
                }
            }
        }
    

提交回复
热议问题