Core Data inserting objects
- by Joe
I'm trying to get my head around Core Data on the iphone.
This is code from Apple's 'Navigation based app using Core data' template (method - 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];
It seems completely counter intuitive to me that the fetched results controller is used when inserting a new object.
I changed the code to this:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:managedObjectContext];
which works just as well and does not require access to the fetch request.
Am I missing something here? Is there any good reason to use the fetched results controller in the insert method?