如何将数据加载到Core Data中?
谢谢你的帮助。
我正在尝试将核心数据添加到我的项目中,但我陷入了在何处以及如何将实际数据添加到持久存储中的问题(我假设这是原始数据的位置)。
我将有 1000 <对象,所以我不想使用 plist 方法。从我的搜索来看,似乎有 xml 和 csv 方法。有什么办法可以使用 SQL 进行输入吗?
用户不会更改数据,并且数据文件将手动输入,因此我不需要在运行时更新这些文件,此时我不限制任何类型的文件 - 最轻的语法是首选。
再次感谢您的帮助。
thanks for you help.
I'm attempting to add core data to my project and I'm stuck at where and how to add the actual data into the persistent store (I'm assuming this is the place for the raw data).
I will have 1000 < objects so I don't want to use a plist approach. From my searches, there seems to be xml and csv approaches. Is there a way I can use SQL for input?
The data will not be changed by the user and the data file will be typed in by hand, so I won't need to update these files during runtime, and at this point I am not limited in any type of file - the lightest on syntax is preferred.
Thanks again for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从 xml/csv/json 文件加载数据,并在应用程序的第一次午餐时创建数据库(如果数据库不存在,则读取数据并创建它)。
更好/更快的方法可能是在应用程序中提供 sqllite DB。您可以在模拟器上以所需的任何格式解析文件,使用所有实体创建一个数据库,然后从 ApplicationData 中获取它,并将其作为资源添加到您的应用程序中。
You could load your data from an xml/csv/json file and create the DB on the first lunch of your application (if the DB is not there, then read the data and create it).
A better/faster approach might be to ship your sqllite DB within your application. You can parse the file in any format you want on the simulator, create a DB with all your entities, then take it from the ApplicationData and just add it to your app as a resource.
尽管我确信可以使用更轻量的文件类型,但我会将 JSON 文件包含到从中导入初始数据集的应用程序包中。
更新:有些人推荐 XML。 NSXMLParser 几乎与 JSONKit 一样快(但比大多数其他解析器快得多),但 XML 语法比 JSON 更重。因此,保存初始数据集的 XML 捆绑文件的权重将高于 JSON 格式的文件。
Although I'm sure there are lighter file types that could be used, I would include a JSON file into the app bundle from which you import the initial dataset.
Update: some folks are recommending XML. NSXMLParser is almost as fast as JSONKit (but much faster than most other parsers), but the XML syntax is heavier than JSON. So an XML bundled file that holds the initial dataset would weight more than if it was in JSON.
考虑到 Apple 考虑其持久存储实现细节的格式,交付预制的 SQLite 数据库并不是一个好主意。即,字段和表的名称可能在 iOS 版本/手机/您能想到的任何隐藏变量之间发生变化。一般来说,您不应该关心数据序列化的格式。
Apple 开发者网站上有一篇关于导入数据的简短文章: 高效导入数据
您应该以您喜欢的任何格式传送初始数据(XML 允许您有效地进行增量解析,从而减少内存占用)并编写一个导入例程如果需要导入数据,请运行。
编辑:考虑到 EliBud 的评论,我仍然认为该方法有点“不确定”... Core Data 使用的 SQLite 数据库的格式不是您想要自己生成的东西(简而言之,这很奇怪,而且仍然不是您真正应该依赖的东西。)。
因此,您需要使用在模拟器上运行的模拟应用程序,并使用核心数据来创建数据库(根据 EliBud 的回答)。但您仍然需要将数据导入那个模拟应用程序!虽然在“真实”计算机上执行一次而不是在移动设备上多次执行此操作可能有意义(即复制文件很容易,导入数据很困难),但您本质上是使用模拟器作为管理工具。
但是嘿,如果它有效的话......
Considering Apple considers the format of its persistent stores implementation details, shipping a prefabricated SQLite database is not a very good idea. I.e. the names of fields and tables may change between iOS versions/phones/whatever hidden variable you can think of. You should, in general, not concern yourself with how this serialization of your data is formatted.
There's a brief article about importing data on Apple's developer site: Efficiently Importing Data
You should ship initial data in whatever format you're comfortable with (XML allows you to do incremental parsing efficiently, which reduces memory footprint) and write an import routine to run if you need to import data.
Edit: With EliBud's comment in mind, I still consider the approach a bit "iffy"... The format of the SQLite database used by Core Data is not something you'd want to generate by yourself (it's weird, simply put, and still not something you should really rely on).
So you'd want to use a mock app running on the Simulator and use Core Data to create the database (as per EliBud's answer). But you'd still have to import the data into that mock-app! And while it might make sense to do this once on a "real" computer instead of a lot of times on a mobile device (i.e. copying a file is easy, importing data is hard), you're essentially using the Simulator as an administration tool.
But hey, if it works...