Core Data inserting objects

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-04-12T10:59:40Z Indexed on 2010/04/12 11:03 UTC
Read the original article Hit count: 332

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c