最近,一些用户更新到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超过X%的时间超过Y秒。
以下是添加和删除两条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级别?