gpt4 book ai didi

ios - 来自调试器 : Terminated due to memory issue 的消息

转载 作者:IT王子 更新时间:2023-10-29 05:33:44 24 4
gpt4 key购买 nike

我的应用程序使用 Geojson 文件。我用 MapBox SDK添加 MGLPolyline 到 map 。但问题是我的文件太大,以至于应用程序崩溃并收到错误:来自调试器的消息:由于内存问题而终止。我在第一个循环中遇到了 66234 对象。我试图将数组分 block 到新数组但没有成功。请帮我解决这个问题。这是我在 map 上绘制的代码,这是我的 test project on github use Xcode 8.1 如果有任何不同的第三方可以解决我的问题也欢迎:

func drawPolyline() {

// Parsing GeoJSON can be CPU intensive, do it on a background thread
DispatchQueue.global(qos: .background).async {
// Get the path for example.geojson in the app's bundle
let jsonPath = Bundle.main.path(forResource: "KMLMAPNew", ofType: "json")
let jsonData = NSData(contentsOfFile: jsonPath!)

do {
// Load and serialize the GeoJSON into a dictionary filled with properly-typed objects
guard let jsonDict = try JSONSerialization.jsonObject(with: jsonData! as Data, options: []) as? Dictionary<String, AnyObject>, let features = jsonDict["features"] as? Array<AnyObject> else{return}

for feature in features {
guard let feature = feature as? Dictionary<String, AnyObject>, let geometry = feature["geometry"] as? Dictionary<String, AnyObject> else{ continue }

if geometry["type"] as? String == "LineString" {
// Create an array to hold the formatted coordinates for our line
var coordinates: [CLLocationCoordinate2D] = []

if let locations = geometry["coordinates"] as? Array<AnyObject> {
// Iterate over line coordinates, stored in GeoJSON as many lng, lat arrays
for location in locations {
// Make a CLLocationCoordinate2D with the lat, lng
if let location = location as? Array<AnyObject>{
let coordinate = CLLocationCoordinate2DMake(location[1].doubleValue, location[0].doubleValue)

// Add coordinate to coordinates array
coordinates.append(coordinate)
}
}
}

let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))

// Optionally set the title of the polyline, which can be used for:
// - Callout view
// - Object identification
line.title = "Crema to Council Crest"

// Add the annotation on the main thread
DispatchQueue.main.async {
// Unowned reference to self to prevent retain cycle
[unowned self] in
self.mapboxView.addAnnotation(line)
}
}
}
}
catch
{
print("GeoJSON parsing failed")
}
}
}

编辑::@Alessandro Ornano 和@fragilecat 非常感谢。但是这些解决方案仍然无法解决应用程序在 iPad 上的终止问题。我认为很难更改当前代码以使其正常工作,因为数据太大了。我想我需要另一种适用于大数据的解决方案。就像将数组分 block 成小数组然后按队列加载它们一样。但我不知道如何开始:(

我给 MapBox 的支持团队发了一封电子邮件,征求建议。

最佳答案

我从创建内存密集型应用程序中学到的一件事是,如果这些循环很长,则每次在循环内创建变量时都必须使用 autoreleasepool

检查你所有的代码并转换类似的东西

func loopALot() {
for _ in 0 ..< 5000 {
let image = NSImage(contentsOfFile: filename)
}
}

进入

func loopALot() {
for _ in 0 ..< 5000 {
autoreleasepool {
let image = NSImage(contentsOfFile: filename)
}
}
}

复习各种循环forwhile

这将强制 iOS 在循环的每一轮结束时释放变量及其对应的内存使用量,而不是持有变量及其内存使用量直到函数结束。这将大大减少您的内存使用量。

关于ios - 来自调试器 : Terminated due to memory issue 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40398746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com