首页 > 解决方案 > Mapkit 在 IOS 13 中使用过多的 CPU

问题描述

最近,在一些用户更新到 iOS 13.x 后,我的 iOS 应用程序开始频繁崩溃(它在 iOS 12.x 中没有出现问题)

我正在使用 Mapkit 渲染一些 MKPolygons 和 MKPolylines。

MKPolylines 每秒大约 10 次被删除并重新添加到 MKMapView(这是因为我的 GPS 以 10Hz 的频率为我提供了新的纬度/经度)。

我在 iOS 12.x 中分析了我的应用程序,它使用了大约 15% 到 30% 的 CPU 资源(iPad Mini 4)。

我在 iOS 13.1.2 和 13.1.3 中进行了相同的分析,CPU 消耗高达 150% 左右(有 2 个 CPU 内核)。

如果我删除以 10Hz 呈现 2 条 MKPolylines 的代码,CPU 利用率会降到 iOS 12.x 的水平。

在 iOS 13.x 中,我的应用程序被 iOS 杀死,并且在崩溃日志中报告原因如下:CPU 在 Y 秒内超过 X%。

这是添加和删除两个 MKPolyLines 的代码:

 func updateLinesToField(myLocation: CLLocationCoordinate2D, field : Site) {
        guard let field = State.shared.currentField else {
            return
        }

        if let _ = closestPassPoint {
            if passLine != nil {
                //calls MKMapView's removeOverlay method
                mapView.remove(passLine)
            }
            if field.state != .completed && myLocation.distanceTo(closestPassPoint) <= Settings.shared.passExtensionLengthMeters {
                passLine = StyledPolyLine(coordinates: [myLocation, closestPassPoint], count: 2)
                passLine.styleAsLineToPass()
                //calls MKMapView's addOverlay method
                mapView.add(passLine)
            }
        }

        if routeLine != nil {
            //calls MKMapView's removeOverlay method
            mapView.remove(routeLine)
        }

        if Settings.shared.showLineToField == .show && field.state != .completed {

            guard State.shared.currentField.passes != nil else {
                return
            }

            let currentPassIndex = State.shared.clipPassIndex()
            let currentPass = State.shared.currentField.passes[currentPassIndex]

            if let nearestPassCoords = currentPass.approachPoints {
                let dist0 = State.shared.currentLocation.distanceTo(nearestPassCoords[0])
                let dist1 = State.shared.currentLocation.distanceTo(nearestPassCoords[1])
                if dist0 < dist1 {
                    routeLine = StyledPolyLine(coordinates: [myLocation, nearestPassCoords[0]], count: 2)
                } else {
                    routeLine = StyledPolyLine(coordinates: [myLocation, nearestPassCoords[1]], count: 2)
                }
                routeLine.styleAsLineToField()
                //calls MKMapView's addOverlay method
                mapView.add(routeLine)
            }
        }
    }

StyledPolyLine 只是继承自 MKPolyline:

class StyledPolyLine: MKPolyline {
    var strokeColor: UIColor!
    var lineWidth: CGFloat! // defaults to 0, which is MKRoadWidthAtZoomScale(currentZoomScale)
    var lineDashPattern: [NSNumber]! // defaults to nil


    func renderer() -> MKPolylineRenderer {
        let _renderer = MKPolylineRenderer(overlay: self)
        _renderer.strokeColor = self.strokeColor
        _renderer.lineWidth = self.lineWidth
        _renderer.lineDashPattern = self.lineDashPattern
        /*
        if #available(iOS 13.0, *) {
            _renderer.shouldRasterize = true
        }
         */
        return _renderer
    }
}

请注意,我尝试将 shouldRasterize 参数更改为 true,但它对 CPU 利用率没有影响。

有什么想法可以让我的 CPU 使用率恢复到 iOS 12.x 级别吗?

标签: iosswiftmkmapviewios13mkpolyline

解决方案


推荐阅读