Inserting New Managed Objects

To create a new instance of a managed object for data that does not yet exist in your model, a reference to the managed object context is needed. The sample app uses a constant to refer to the ICFAppDelegate instance, which has a property defined for the managed object context:

NSManagedObjectContext *moc = [kAppDelegate managedObjectContext];

To insert data, Core Data needs to know what entity the new data is for. Core Data has a class called NSEntityDescription that provides information about entities. Create a new instance using NSEntityDescription’s class method:

NSManagedObject *newMovie1 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" ...

Get iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.