预先存在的 Core-Data 数据
我环顾四周,但没有找到我要找的东西。我需要一些数据基本上预先加载到应用程序中。我知道我可以在第一次启动时将其全部放入,但希望避免在第一次启动时长时间加载并使其已经加载。
是否可以将实体插入核心数据中,以便对它们进行硬编码?
I've looked around for this but haven't found what I'm looking for. I need some data to basically come pre-loaded in the app. I know that I could just put it all in on the first launch but would like to stay away from a long load time on the first launch and have it already loaded.
Is it possible to insert entities into core-data so that they are hard-coded in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您在应用程序包中包含一个预填充的数据存储,并将其从包中复制到文档目录,作为应用程序启动过程的一部分 - 检查数据存储是否存在,如果不存在,则进行复制。您可以在首次访问核心数据堆栈之前执行此操作。
有几种方法可以做到这一点。懒惰的程序员方法是在手机或模拟器中将默认数据输入到应用程序中,获取数据存储文件,并将其包含在您的 Xcode 项目中。缺点是如果您稍后需要返回并编辑数据模型,它就不能很好地工作。
另一种选择是在 Mac 上创建一个编辑器应用程序,该应用程序使用与 iPhone 应用程序相同的 Core Data 模型(它们是兼容的),并在 Mac 应用程序中编辑数据。 Jeff Lamarche 在他的一篇博客文章<中对此进行了一些讨论< /a>.我也做过类似的事情,只不过我编写了一个命令行工具来从网站下载最新数据(在我的例子中是 XML 数据)并将 XML 解析为 NSManagedObjects。
这篇 StackOverflow 帖子讨论了更复杂的选择是拥有两个数据存储 - 一个用于系统数据,一个用于用户数据 - 并让 Core Data 在运行时使用这两个存储。
Yeah, you include a a pre-filled data store in your app bundle and copy it from the bundle to the documents directory as part of the app launch process - check if the data store exists and, if not, do the copy. You do this prior to accessing the Core Data stack for the first time.
There are a few ways you could do this. The lazy programmer way is to enter your default data into the app, either on the phone or in the simulator, grab the data store file, and include it in your Xcode project. The downside is it doesn't work well if you need to go back and edit the data model later.
The other option is to create an editor app on the Mac that uses the same Core Data model as your iPhone app (they're compatible) and edit the data in your Mac app. Jeff Lamarche talks a bit about this in one of his blog postings. I've done something similar, except I wrote a command line tool to download the latest data from a web site (in my case, XML data) and parse the XML into NSManagedObjects.
This StackOverflow post talks about a bit more complex option of having two data stores - one for your system data and one for your user data - and letting Core Data use both stores at runtime.