删除核心数据中的对象ios
问题描述:
从核心数据删除记录时,我得到EXC_BAD_INSTRUCTION Error
。下面是代码:删除核心数据中的对象ios
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
有您使用此代码。即使越来越崩溃。可能是被are.you要获取数据与您的谓词的谓词的一个越来越空值的问题从具有空值的核心数据中获得,这在数据库中是不可用的。
答
而不是使用下面的方法删除
context.delete(candles[indexPath.row] as! NSManagedObject)
你应该使用
context.deleteObject(candles[indexPath.row] as! NSManagedObject)
- (void)delete:(id)sender
:从用户界面删除的选择。 当用户点击编辑菜单的删除命令时,将调用此方法。 UIResponder的一个子类通常通过从用户界面中删除选定的对象以及(如果适用)从应用程序的数据模型中实现此方法。它不应该写入任何数据到粘贴板。该命令从响应者链中的第一响应者一直到处理;如果没有响应者处理它,它将被忽略。如果响应者在当前上下文中不处理该命令,则应将其传递给下一个响应者。
- (void)deleteObject:(NSManagedObject *)object
:指定在提交更改时应从其持久存储区中删除的对象。 提交更改时,对象将从uniquing表中删除。如果对象尚未保存到持久性存储中,则只需将其从接收器中删除即可。
请参阅此链接:http://*.com/questions/37559445/how-to-delete-row-from-coredata-entity-ios-swift/37559834#37559834 –