ios - 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 级别吗?
解决方案
推荐阅读
- c# - 在邮递员中工作的 Restful API,C# 不工作 [Magento 集成]
- node.js - LOOPBACK 4:在 API 调用中添加参数
- linux - 从 bash 脚本中的其他变量声明中导出具有变量名称和值的命令
- c# - 我需要处理嵌套例程的变量吗?
- php - 在 Codeigniter 中调用未定义的方法
- python-3.x - 如何将给定列表中的每个元素替换为其正方形而不是使用正方形创建新列表
- php - Laravel 模型观察者事件不适用于集合方法调用
- c# - 如何将打字稿中的日期转换为 c# 并返回正确的时区?
- javascript - 单击两个链接时显示一个 div
- text - 如何在 tikz 中缩放条形图而不缩放文本?