gpt4 book ai didi

ios - 核心数据内存崩溃

转载 作者:行者123 更新时间:2023-11-28 16:08:57 25 4
gpt4 key购买 nike

<分区>

我是第一次在我的项目中使用 Core Data,我觉得我的方法存在严重问题。我正在做的是从服务器获取数据(数据也包括 png)。将其保存在本地的核心数据中。然后在应用程序启动时,我将整个数据加载到一个数组中。然后在我需要的地方使用这个数组。我认为我采用了一种非常糟糕的方法。谁能指导我什么是更好的方法?我应该只在需要数据时查询核心数据,而不是在开始时将所有内容都加载到内存中吗?

当数据被填充到数组时,我可以看到 Xcode 中的内存增加,并且在达到某个值后,它崩溃了。

这是我保存数据的代码:

func saveDataLocally () {

let moContext = ((UIApplication.shared.delegate) as! AppDelegate).managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "FoodPlace", in: moContext)

for foodPlaceData in self.downloadedData_ {
let foodPlace = NSManagedObject(entity: entity!, insertInto: moContext) as! FoodPlace
foodPlace.objectId = foodPlaceData.objectId_
foodPlace.name = foodPlaceData.name_
foodPlace.address = foodPlaceData.address_
foodPlace.keywords = foodPlaceData.keywords_
foodPlace.baseFavourites = Int64(foodPlaceData.baseFavourites_)
foodPlace.startingTime = foodPlaceData.startingTime_
foodPlace.endingTime = foodPlaceData.endingTime_
foodPlace.category = foodPlaceData.category_
foodPlace.basePrice = foodPlaceData.basePrice_
foodPlace.dealTitle = foodPlaceData.dealTitle_
foodPlace.versionNumber = Int64(foodPlaceData.versionNumber_)
foodPlace.menuItems = NSKeyedArchiver.archivedData(withRootObject: foodPlaceData.menuItems_)
foodPlace.location = NSKeyedArchiver.archivedData(withRootObject: foodPlaceData.location_)
foodPlace.deals = NSKeyedArchiver.archivedData(withRootObject: foodPlaceData.deals_)
foodPlace.foodPlacePhotos = NSKeyedArchiver.archivedData(withRootObject: foodPlaceData.foodPlacePhotos_)

moContext.insert(foodPlace)
}

do {
try moContext.save()
}
catch let error {
print("error saving = \(error.localizedDescription)")
}
}

其中 menuItems 是一个包含文本和 png 图像的 Dictionary。此外,dealsfoodPlacePhotos 仅包含 png 图像。

获取代码如下:

func loadDataLocally () {
let moContext = ((UIApplication.shared.delegate) as! AppDelegate).managedObjectContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "FoodPlace")

do {
let results = try moContext.fetch(request)
let savedFoodPlaceData = results as! [FoodPlace]

downloadedData_ = []

for foodPlace in savedFoodPlaceData {
let objectId = foodPlace.objectId
let name = foodPlace.name
let address = foodPlace.address
let keywords = foodPlace.keywords
let baseFavourites = foodPlace.baseFavourites
let startingTime = foodPlace.startingTime
let endingTime = foodPlace.endingTime
let category = foodPlace.category
let menuItems = NSKeyedUnarchiver.unarchiveObject(with: foodPlace.menuItems!) as? [Dictionary<String,AnyObject>]
let location = NSKeyedUnarchiver.unarchiveObject(with: foodPlace.location!) as? Dictionary<String,Double>
let deals = NSKeyedUnarchiver.unarchiveObject(with: foodPlace.deals!) as? [UIImage]
let basePrice = Float(foodPlace.basePrice)
let dealTitle = foodPlace.dealTitle
let versionNumber = foodPlace.versionNumber
let foodPlacePhotos = NSKeyedUnarchiver.unarchiveObject(with: foodPlace.foodPlacePhotos!) as? [UIImage]

let data = FoodPlaceData(objectId: objectId!, name: name!, address: address!, category: category!, keywords: keywords!, baseFavourites: Int(baseFavourites), startingTime: startingTime!, endingTime: endingTime!, menuItems: menuItems!, location: location!, deals: deals!,basePrice: basePrice,dealTitle: dealTitle!,versionNumber: Int(versionNumber),foodPlacePhotos: foodPlacePhotos!)

downloadedData_.insert(data, at: downloadedData_.count)
}
}
catch let error {
print("error fetching = \(error.localizedDescription)")
}
}

下面是删除数据的代码:

func deleteAllLocalData () {
let moContext = ((UIApplication.shared.delegate) as! AppDelegate).managedObjectContext

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "FoodPlace")
fetchRequest.returnsObjectsAsFaults = false

do {
let results = try moContext.fetch(fetchRequest)

for managedObject in results {
let managedObjectData : NSManagedObject = managedObject as! NSManagedObject
moContext.delete(managedObjectData)
}

try moContext.save()

} catch let error {
print("Delete all data in FoodPlace error : \(error) \((error as NSError).userInfo)")
}
}

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