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;
}
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:
Hope this helps. Good luck.