Question

In my managedObjectModel I have several Entities. I think that it's possible to create ManagedObjectContext for each one... If it's true I would like to know:

  1. Do I need method - (NSManagedObjectModel *)managedObjectModel for each ManagedObjectContext?

  2. Do I need method - (void)saveContext for each ManagedObjectContext?

Was it helpful?

Solution

You don't need to do this. A single managed object context is designed to deal with multiple entities, and if there are relationships between your entities, they have to be in the same context.

Separate contexts are useful for background threading and temporary workspaces, as explained rather well here. Until and unless you need that, stick with a single context.

OTHER TIPS

Read the Core Data Basics again and take a look at some sample code that uses Core Data.

Usually,

  • You have one NSManagedObjectModel that is like a schema which provides the descriptions of your entities.
  • You have one NSPersistentStoreCoordinator which uses your NSManagedObjectModel to create the DB
  • And you have one or more NSManagedObjectContext which are 'scratch pads' which talk to your NSPersistentStoreCoordinator and manage a context of objects. Think of it like a way to have a transaction.

And when you make a project, be sure to check 'Use Core Data' so the project comes with most of it set up for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top