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

倾然丶 夕夏残阳落幕 提交于 2019-12-07 14:24:44

问题


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:


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.



来源:https://stackoverflow.com/questions/3187702/using-two-different-core-data-models-in-the-same-iphone-app-how-can-i-do-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!