Monday, February 21, 2011

Using two different Core Data Models in the same iPhone app. How can I do it?

I am trying to use two different Core Data Models in a iPhone application, I created and correctly set up the first Core Data Model that uses SQLite as persistent object store. This one works very well and the pre-populated default store loads correctly in a Table View.

Now I want to create a different Core Data Model with a different pre-populated SQLite default store to load it in a different Table View in the same iPhone application. How can I do this task? I read the Core Data documentation and downloaded the sample codes but I did not find anything about this task.

Any sample code useful to solve this problem will be appreciated.

Thank you in advance, Pier

From stackoverflow
  • You can do it two different ways:

    • You can set up a separate entire core data stack, effectively just copying the template code you already have in your AppDelegate.

    • You can add the second Core Data sqlite file into the existing core data stack. This will let you access both Entities (not tables, this is an object graph not a database) in the same stack. To do that you add a second -addPersistentStore... call in your -persistentStoreCoordinator method and make sure your -managedObjectModel method is doing a merge of the models in your bundle.

    Update

    Set it up anywhere you want. You can set it up in the AppDelegate and then do a dependency injection and push down the second stack to whoever needs reference to it.

    Generally I would not create the stack in the UIViewController as it is not its responsibility.

    Pier : Thank you very much, but I did not understand where do I set up the separate entire core data stack, using the template code that I have in my AppDelegate? Maybe in the table view controller where I want to load the data of the second Core Data Stack ?

0 comments:

Post a Comment