我需要处理核心数据崩溃。我的代码在 managedObjectContext.save() 上崩溃了。 但是 catch block 没有捕获到任何异常。为了避免崩溃,我怎样才能更好地编写我的 Catch block 这是我的代码。
do {
try managedObjectContext.save()
}
catch let error as NSError {
Print.print("Error saving data store: \(error)")
}
最佳答案
这是使用 CoreData 保存数据的示例。这可能对您有所帮助。
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if let entity = NSEntityDescription.entity(forEntityName: "Employees", in: context){
let myItem = NSManagedObject(entity: entity, insertInto: context)
myItem.setValue(nameTF.text, forKey: "names")
myItem.setValue(15655, forKey: "mobileNo")
do {
try context.save()
}catch let nserror as NSError{
print("ERROR: Coredata error \(nserror)")
}
}
关于ios - 如何处理 iOS Swift 中的 CoreData 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286431/