Reputation: 65
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
Upvotes: 3
Views: 1140
Reputation: 46718
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.
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.
Upvotes: 2