crash in Coca Touch when adding two objects to an empty core-data store with a UITableView using sections
crash in Coca Touch when adding two objects to an empty core-data store with a UITableView using sections
- Subject: crash in Coca Touch when adding two objects to an empty core-data store with a UITableView using sections
- From: Jean-Denis Muys <email@hidden>
- Date: Mon, 15 Mar 2010 03:27:53 +0100
This is a Cocoa Touch question. Is this list also for Cocoa Touch?
I can't manage to solve a crash that occurs with a very limited modification to a project as generated by XCode.
When adding two (or more) objects to an empty store rather than one, when using sections, Cocoa Touch crashes with an array out-of-bound exception.
I have spent a large number of hours on this, but I am going nowhere, so maybe someone here has an idea.
To reproduce the crash:
1- Start with a Navigation-based application project using Core Data. I named it crashTest.
2- The template has one Entity in the data model, called Event. Add two strings attributes to it. I added "name" and "kind".
3- Modify RootViewController.m to use sections on the new "kind" attribute. to do so change one line in - (NSFetchedResultsController *)fetchedResultsController:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
to:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"kind" cacheName:@"Root"];
Also, one data source routine must be added to name the section:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[fetchedResultsController sections] objectAtIndex:section] name];
}
4- Modify the - (void)insertNewObject routine to create two objects at once, and to set the two new attributes:
- (void)insertNewObject {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
[newManagedObject setValue:@"name1" forKey:@"name"];
[newManagedObject setValue:@"lunch" forKey:@"kind"];
newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
[newManagedObject setValue:@"name2" forKey:@"name"];
[newManagedObject setValue:@"lunch" forKey:@"kind"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
5- Run, tap the "+" button, and watch it crash:
2010-03-15 02:48:30.056 crashTest[12767:207] Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
At this point, I am thinking about a bug in Cocoa Touch. But I may also have overseen something. What?
In any case, the only workaround I found is to setup a flag to YES (say "massUpdate"), and in the four delegate routines, test for this flag. If set do nothing, except reload the whole table view data instead of calling endUpdate.
Many thanks,
Jean-Denis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden