正在删除coreData ios中的对象



从核心数据中删除一条记录时,我正在获取EXC_BAD_INSTRUCTION Error。这是代码:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
//deleteIncidents()
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
context.delete(candles[indexPath.row] as! NSManagedObject)
candles.removeAtIndex(indexPath.row)
do{
try context.save()
}
catch{
}
//tableView.reloadData()
// remove the deleted item from the `UITableView`
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext

你用过这个代码吗?甚至会崩溃。可能是你的谓词有问题,你的一个谓词得到了null值。你想从数据库中没有的null值的核心数据中获取数据。

使用以下方法删除

context.delete(candles[indexPath.row] as! NSManagedObject)

你应该使用

context.deleteObject(candles[indexPath.row] as! NSManagedObject)

- (void)delete:(id)sender:从用户界面中删除所选内容。当用户点击编辑菜单的Delete命令时,会调用此方法。UIResponder的子类通常通过从用户界面中删除所选对象来实现此方法,如果适用,还可以从应用程序的数据模型中删除。它不应该向粘贴板写入任何数据。命令从第一响应器向上传播到响应器链,直到它被处理;如果没有响应程序处理它,它将被忽略。如果响应程序没有在当前上下文中处理命令,它应该将其传递给下一个响应程序。

- (void)deleteObject:(NSManagedObject *)object:指定在提交更改时应从其持久存储中删除的对象。提交更改后,对象将从uniquing表中删除。若对象尚未保存到持久存储中,则只需从接收器中删除它。

最新更新