在同一个 iPhone 应用程序中使用两个不同的核心数据模型。我该怎么做呢?
我试图在 iPhone 应用程序中使用两种不同的核心数据模型,我创建并正确设置了第一个使用 SQLite 作为持久对象存储的核心数据模型。这个效果很好,预填充的默认存储在表视图中正确加载。
现在,我想使用不同的预填充 SQLite 默认存储创建不同的核心数据模型,以将其加载到同一 iPhone 应用程序的不同表视图中。 我怎样才能完成这个任务?我阅读了 Core Data 文档并下载了示例代码,但没有找到有关此任务的任何内容。
任何有助于解决此问题的示例代码将不胜感激。
先感谢您, 码头
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过两种不同的方式来实现:
您可以设置一个单独的整个核心数据堆栈,只需复制 AppDelegate 中已有的模板代码即可。
您可以将第二个 Core Data sqlite 文件添加到现有的核心数据堆栈中。这将允许您访问同一堆栈中的两个实体(不是表,这是对象图而不是数据库)。为此,您在
-persistentStoreCoordinator
方法中添加第二个-addPersistentStore...
调用,并确保您的-managementObjectModel
方法正在执行合并捆绑包中的模型数量。更新
将其设置在您想要的任何地方。您可以在 AppDelegate 中设置它,然后进行依赖项注入,并将第二个堆栈下推给需要引用它的人。
一般来说,我不会在
UIViewController
中创建堆栈,因为这不是它的责任。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.