2016-06-07 150 views
0

從核心數據刪除記錄時,我得到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) 

    } 
} 
+0

請參閱此鏈接:http://stackoverflow.com/questions/37559445/how-to-delete-row-from-coredata-entity-ios-swift/37559834#37559834 –

回答

0
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
let context:NSManagedObjectContext = appDel.managedObjectContext 

有您使用此代碼。即使越來越崩潰。可能是被are.you要獲取數據與您的謂詞的謂詞的一個越來越空值的問題從具有空值的核心數據中獲得,這在數據庫中是不可用的。

0

而不是使用下面的方法刪除

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

你應該使用

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

- (void)delete:(id)sender:從用戶界面刪除的選擇。 當用戶點擊編輯菜單的刪除命令時,將調用此方法。 UIResponder的一個子類通常通過從用戶界面中刪除選定的對象以及(如果適用)從應用程序的數據模型中實現此方法。它不應該寫入任何數據到粘貼板。該命令從響應者鏈中的第一響應者一直到處理;如果沒有響應者處理它,它將被忽略。如果響應者在當前上下文中不處理該命令,則應將其傳遞給下一個響應者。

- (void)deleteObject:(NSManagedObject *)object:指定在提交更改時應從其持久存儲區中刪除的對象。 提交更改時,對象將從uniquing表中刪除。如果對象尚未保存到持久性存儲中,則只需將其從接收器中刪除即可。

相關問題