'断言失败'删除CoreData / UICollectionView中的多个对象 - Swift

时间:2014-08-30 17:17:02

标签: ios core-data uicollectionview nsfetchedresultscontroller nsmanagedobjectcontext

我正在尝试删除我的UICollectionView中的多个选择项:

projectIndxPthsToDelete 包含在我的UICollectionView上突出显示的所有indexPaths以供删除。

要删除,我运行以下代码:

for var x = 0; x < projectsIndxPthsToDelete.count; x++ {
    self.managedObjectContext.deleteObject(self.fetchedResultsController?.objectAtIndexPath(self.projectsIndxPthsToDelete.objectAtIndex(x) as NSIndexPath) as NSManagedObject)
}
self.managedObjectContext.save(nil)

当调用 self.managedObjectContext.save(nil)时,它会进入下面的didChangeObject委托:

func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {

    switch type {
    case NSFetchedResultsChangeType.Delete:
        self.collectionView.deleteItemsAtIndexPaths(self.projectsIndxPthsToDelete)

    default:
        println("didChangeObject ERROR")
    }

}

我遇到的问题是,当我有一个要删除的indexPath时,它可以正常工作。但是,当我的 self.projectIndxPthsToDelete 数组中有多个indexPath时,删除会引发以下异常:

Assertion failure in -[UICollectionView _endItemAnimations] Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (4) must be equal to the number of items contained in that section before the update (4), plus or minus the number of items inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

以及:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (4) must be equal to the number of items contained in that section before the update (4), plus or minus the number of items inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' 

为什么只有当我尝试一次删除多个内容时才会发生这种情况?我该如何解决这个问题?

0 个答案:

没有答案