0

I'm trying to delete multiple select items in my UICollectionView:

projectIndxPthsToDelete contains all of the indexPaths that have bee highlighted on my UICollectionView for deletion.

To delete, I run the following code:

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)

When self.managedObjectContext.save(nil) is called, it then goes into my didChangeObject delegate below:

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")
    }

}

The issue I have is that when I have one indexPath to delete, it works fine. However when I have multiple indexPaths in my self.projectIndxPthsToDelete array, deleting throws the following exception:

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).

As well as:

 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).' 

Why is this happening only when i try to delete more than one thing at once? How can i fix this problem?

Fudgey
  • 3,793
  • 7
  • 32
  • 53
  • 1
    Compare http://stackoverflow.com/questions/20554137/nsfetchedresultscontollerdelegate-for-collectionview . – Martin R Aug 30 '14 at 17:22
  • @MartinR If using NSFetchedResultsController isn't very compatible with UICollectionView - is there a better system i should be using? – Fudgey Aug 30 '14 at 18:18
  • 1
    You *can* use a fetched results controller with a collection view. It is just a bit more complicated than with a table view. The answer to the question that I linked to contains a link to a sample implementation. – Martin R Aug 30 '14 at 18:52

0 Answers0