views:

36

answers:

1

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

+1  A: 

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.

Marcus S. Zarra
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 ?
Pier