试图从核心数据中删除一个实体,我得到...?

Posted

技术标签:

【中文标题】试图从核心数据中删除一个实体,我得到...?【英文标题】:Trying to delete an entity from core data, i get...? 【发布时间】:2016-01-07 18:08:45 【问题描述】:

我正在尝试从核心数据中删除一个实体,但出现以下错误:

    The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (10), 
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这是我的表格视图功能

   // MARK: delete
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 

        let deletedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

        if editingStyle == UITableViewCellEditingStyle.Delete 

            // remove the deleted item from the model
            moContext.deleteObject(scannedVisitors[indexPath.row] as NSManagedObject)
            scannedVisitors.removeAtIndex(indexPath.row)
            do 
                try moContext.save()
                print("deleted and saved")
             catch 
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                abort()
                 
             // remove the deleted item from the `UITableView`
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            deletedRow.accessoryType = UITableViewCellAccessoryType.None
            print("Deleted + accessory")
        

    

我在 viewDidAppear 中有 fetchRequest 而在 viewDidLoad 中没有

override func viewDidAppear(animated: Bool) 

    do 
        let request =  NSFetchRequest(entityName: "ScannedVisitor")

        scannedVisitors = try moContext.executeFetchRequest(request) as! [ScannedVisitor]

        self.tableView.reloadData()

     catch let error as NSError  
        print(error)
    


当我重新启动应用程序时,实体已被删除,但在删除操作期间出现运行时错误。 我该如何解决这个问题?

[编辑] 仔细观察,我发现这部分被“解析”了 3 次

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    print("Rows \(scannedVisitors.count)")
    return scannedVisitors.count

因为它会打印

Rows 0
Rows 0
Rows 6

【问题讨论】:

【参考方案1】:

不要从视图中获取要删除的对象,从模型中获取它并在保存上下文之前执行有关 UI 的所有删除任务。

let objectToDelete = scannedVisitors[indexPath.row]
scannedVisitors.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)
moContext.deleteObject(objectToDelete)
do 
  try moContext.save()
...

实际上,无论如何都不需要更改将要删除的单元格的附件类型

【讨论】:

以上是关于试图从核心数据中删除一个实体,我得到...?的主要内容,如果未能解决你的问题,请参考以下文章

从核心数据实体中删除对象/行

从核心数据中删除实体

如何从 Swift 的核心数据中删除特定的实体数据?

如何从核心数据中仅获取第一个实体?

SWIFT 的核心数据:如何从关系实体中删除对象?

iOS:删除核心数据中具有一对多关系的实体