gpt4 book ai didi

swift - 在 Swift 中保存核心数据并发时应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:04 24 4
gpt4 key购买 nike

在核心数据中保存时应用程序崩溃

do {
try context.save() <----------- CRASH ---------
} catch {
print("Failed saving")
}

我使用这段代码在后台将数据保存在核心数据中

DispatchQueue.global(qos: .background).async {
// Fetches from database
DispatchQueue.main.async {
if fetchedActivitiesFromDB {
// Updates the view
} else {
// gets from the server
// Save the result in Core Data <------------
let coreData = CoreDataHelper()
coreData.saveInDB(fetchedItemsFromServer)
}
}
}

根据上面的代码,这可以同时保存在 CoreData 中。
我做了一些任务来解决问题,但它们没有用。

首先:我想创建一个队列来处理并发保存

let saveQueue = DispatchQueue(label: "start save in CoreData");
saveQueue.async {
DispatchQueue.global(qos: .background).async{
do {
try context.save()
} catch {
print("Failed saving")
}
}
}

上面的代码没有解决问题。

第二:CoreDataManager 不是单例。我认为这是问题所在,所以我将其更改为:

class CoreDataHelper {

static let sharedInstance = CoreDataHelper()

private init() {}

static func getInstance() -> CoreDataHelper {
return sharedInstance
}
//...
}

第三:我更改了 CoreData Stack 设置

static var managedObjectContext: NSManagedObjectContext = {
let coordinator = persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator

return managedObjectContext
}()

ConcurrencyType 曾经是 ma​​inQueueConcurrencyType

这些更改均未解决问题

崩溃详情:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000014153'
*** First throw call stack:
(0x18485cf48 0x19940ff80 0x184863c5c 0x184860c00 0x184764cac 0x18482c978 0x18476de38 0x18460f134 0x1845ebda0 0x1845ed47c 0x18453f2fc 0x184509f04 0x1845d0fdc 0x1845d8fd8 0x1025b5c68 0x1025c181c 0x1845cb830 0x184509ab0 0x18452e1d8 0x1005bce00 0x10029e498 0x100484a08 0x1003c097c 0x1003ca13c 0x1001296e8 0x1025b5ca8 0x1025b5c68 0x1025bb710 0x1848141f8 0x184812060 0x184740ca0 0x18f97c088 0x189e58ffc 0x10011c8e0 0x199c5e8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

import UIKit
import CoreData

class CoreDataHandler: NSObject {


func getContext() -> NSManagedObjectContext{

let appdelegate = UIApplication.shared.delegate as! AppDelegate

return appdelegate.persistentContainer.viewContext

}

func saveDataToCoreData(username:String,password:String) -> Bool{

let entity = NSEntityDescription.entity(forEntityName:"Entity", in: getContext())
let manageObect = NSManagedObject(entity: entity!, insertInto: getContext())

manageObect.setValue(username,forKey:password)
do{
try getContext().save()

return true
}
catch{
return false
}
}

func fetchAllData() -> [Entity]?{

do{
let result:[Entity]? = try getContext().fetch(Entity.fetchRequest())
return result
}
catch{

return nil
}
}

func delete(ManagedObj:Entity) -> Bool{

getContext().delete(ManagedObj)

do{
try getContext().save()

return true
}
catch{
return false
}


}

func cleanMyCoreData() -> Bool{

let delete = NSBatchDeleteRequest(fetchRequest:Entity.fetchRequest())
do{
try getContext().execute(delete)

return true
}
catch{
return false
}

}

func filterByPredicate(pred:NSPredicate) -> [Entity]?{

let fetchReq:NSFetchRequest<Entity> = Entity.fetchRequest()

fetchReq.predicate = pred

do{

let entity = try getContext().fetch(fetchReq)

return entity

}
catch{
return nil
}


}



}

关于swift - 在 Swift 中保存核心数据并发时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820712/

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