Q: UICollectionView Invalid number of items, arrays adding and deleting elements I am working on an iPhone app with a UICollectionView, and am trying to create folders like the iPhones spring board. I have it working but am running into this error regarding the addition and subtraction of cells using core data. I was hoping someone could explain how I avoid this error? Thank You in advance.
* 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 (3), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
------mainViewController
            //**Document One**
            //get first doc and create it as a sub doc
            Document * docToCopy = [[self birds] objectAtIndex:longPressFolderLayout.pinchedCellPath.row];
            SubDocument *doc = [[SubDocument alloc] initInsertingIntoManagedObjectContext:managedObjectContext];
            [doc setTitle:docToCopy.title];
            [doc setThumbnail:docToCopy.thumbnail];
            [doc setIsFolder:[NSNumber numberWithInt:0]];
            [doc setDate:docToCopy.date];


            //**Document Two**
            //get second doc and create it as a sub doc
            Document * docToCopy2 = [[self birds] objectAtIndex:cellAttributes.indexPath.row];
            SubDocument *doc2 = [[SubDocument alloc] initInsertingIntoManagedObjectContext:managedObjectContext];
            [doc2 setTitle:docToCopy2.title];
            [doc setThumbnail:docToCopy2.thumbnail];
            [doc2 setIsFolder:[NSNumber numberWithInt:0]];
            [doc2 setDate:docToCopy2.date];
            [self add:@"folder" image:[UIImage imageNamed:@"folderDefaultImage"] date:[NSDate date] folder:[NSNumber numberWithInt:1]];

            //**Folder**
            //create a folder and append both sub docs and then remove the original docs
            Document * origFolder = [[self birds] objectAtIndex:([[self birds]count]-1)];
            [origFolder appendDoc:doc];
            [origFolder appendDoc:doc2];
            [origFolder setIsFolder:[NSNumber numberWithInt:1]];
            currentOpenFolder = origFolder;
            [self removePageAtIndex:longPressFolderLayout.pinchedCellPath.row];
            [self removePageAtIndex:cellAttributes.indexPath.row];

------DocumentNSManagedObject
- (void)appendDoc:(SubDocument *)doc
{
[[self mutableSetValueForKey:@"subDocument"] addObject:doc];
[(NSMutableArray *)[self subDocumentsOrdered] addObject:doc];

    NSError *error = nil;
[[self managedObjectContext] save:&error];
}
- (void)deleteDoc:(SubDocument *)doc
{
    [[self mutableSetValueForKey:@"subDocument"] removeObject:doc];
    [(NSMutableArray *)[self subDocumentsOrdered]removeObject:doc];
    NSError *error = nil;
[[self managedObjectContext] save:&error];
}
- (NSArray *)subDocumentsOrdered
{
if (!subDocumentsOrdered)
{
subDocumentsOrdered = [[[[self subDocument] allObjects] sortedArrayWithOptions:NSSortStable|NSSortConcurrent usingComparator:(NSComparator) ^(SubDocument *stroke1, SubDocument *stroke2)
                          {
                              return [[stroke1 date] compare:[stroke2 date]];
                          }
                          ] mutableCopy];
}
    return subDocumentsOrdered;
}


A: The error message is to do with the UICollectionView and the information you are providing it with as you add and remove items or respond to it's UICollectionViewDataSource methods such as collectionView:numberOfItemsInSection:
Basically the actions you are performing on the UICollectionView don't add up.
It's telling you that in Section 0


*

*You start with 3 items in the section, i.e. you responded to collectionView:numberOfItemsInSection: by returning 3.

*You didn't insert or remove any items.

*You now say there are 4 items in the section, i.e. you responded to collectionView:numberOfItemsInSection: by returning 4.


So the UICollectionView is complaining that you telling it there is an extra item in the section but you didn't tell it where to insert it.  So this is what you need to do.  
It doesn't really have much to do with core-data it's to do with the UICollectionView 

A: I had a very similar problem recently in my app. If you look at where you are adding your pages I bet you are not saving them to the NSManagedObjectContext. A simple solution is just by writing:
[[self managedObjectContext]save:nil];

Hope this helps. Good luck.
