最近,在一些用户更新到 iOS 13.x 后,我的 iOS 应用程序开始频繁崩溃(在 iOS 12.x 中没有出现该问题)
我正在使用 Mapkit 渲染一些 MKPolygons 和 MKPolylines。
MKPolylines 被删除并重新添加到 MKMapView 中的速度大约每秒 10 次
(那是因为我的 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 超过 X% 的时间超过 Y 秒。
以下是添加和删除两条 MKPolyLine 的代码:
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 水平吗?