问题描述
我的应用涉及通过互联网获取一个大型 json 文件,然后将其解析为 Core Data.
My app involves getting a large json file via the internet, and then parsing it into Core Data.
那很好,但是我怎么能把这个 Core Data 数据库的已经填充的版本放到我的应用程序中,所以当他们第一次启动它时它就在那里.并且用户可以决定稍后刷新.
Thats fine, but how could I get the already filled version of this Core Data database into my app, so it is there when they first launch it. And the user can decide to refresh it later.
推荐答案
一般——创建一个单独的项目,将JSON文件解析成一个核心数据数据库.创建您的真实项目,将对象模型和数据库文件复制到这个新项目中.
Generally - create a separate project, parse the JSON file into a core data database. Create your real project, copy the Object model and the database file to this new project.
现在,在应用启动时,检查文档目录中是否存在数据库,如果不存在,请从应用程序包中复制预填充的数据库.
Now, at app start up, check if the database exists in the document's directory, and if it does not, copy your prefilled one from your app bundle.
确保 Persistent Store Coordinator 使用文档文件夹中的数据库,而不是应用程序包中的数据库.
Make sure that the Persistent Store Coordinator works with the database in the documents folder and not the one in the app bundle.
2012 年 6 月更新
我在 GitHub 上有一个名为 PromNight 的小示例项目,它演示了使用带有iPad 项目和 OS X 项目,用于预加载 Core Data 的数据.这使用在两个应用程序之间共享的对象模型,这有助于在预加载时保持更改同步.
I've got a small example project on GitHub called PromNight which demonstrates using an Xcode Workspace with an iPad project and a OS X project to preload the data for Core Data. This uses an object model that is shared between the the two applications which helps to keep changes in sync when preloading.
这篇关于Core Data 的预填充版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!