简体   繁体   English

如何将数据加载到Core Data?

[英]How to load data into Core Data?

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. 我将有1000个<对象,所以我不想使用plist方法。 From my searches, there seems to be xml and csv approaches. 从我的搜索,似乎有xml和csv方法。 Is there a way I can use SQL for input? 有没有办法使用SQL进行输入?

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. 再次感谢任何帮助。

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). 您可以从xml / csv / json文件加载数据,并在应用程序的第一个午餐时创建数据库(如果数据库不在那里,则读取数据并创建它)。 A better/faster approach might be to ship your sqllite DB within your application. 更好/更快的方法可能是在您的应用程序中发布sqllite数据库。 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. 您可以在模拟器上以任何格式解析文件,使用所有实体创建数据库,然后从ApplicationData中获取它,并将其作为资源添加到您的应用程序中。

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. 虽然我确定可以使用较轻的文件类型,但我会在应用程序包中包含一个JSON文件,您可以从中导入初始数据集。

Update : some folks are recommending XML. 更新 :有些人推荐使用XML。 NSXMLParser is almost as fast as JSONKit (but much faster than most other parsers), but the XML syntax is heavier than JSON. NSXMLParser几乎和JSONKit一样快(但比大多数其他解析器快得多),但XML语法比JSON重。 So an XML bundled file that holds the initial dataset would weight more than if it was in JSON. 因此,保存初始数据集的XML捆绑文件比JSON中的重量更大。

Considering Apple considers the format of its persistent stores implementation details, shipping a prefabricated SQLite database is not a very good idea. 考虑到Apple考虑其持久存储实现细节的格式,运送预制的SQLite数据库并不是一个好主意。 Ie the names of fields and tables may change between iOS versions/phones/whatever hidden variable you can think of. 即,iOS版本/手机/您可以想到的任何隐藏变量之间的字段和表格的名称可能会发生变化。 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 有一篇关于在Apple开发者网站上导入数据的简短文章: 有效导入数据

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. 您应该以您认为合适的任何格式发送初始数据(XML允许您有效地进行增量解析,从而减少内存占用)并编写导入例程以在需要导入数据时运行。

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 ). 编辑:考虑到EliBud的评论,我仍然认为这个方法有点“不确定”...... Core Data使用的SQLite数据库的格式不是你想要自己生成的东西(它很奇怪,简单地说,和仍然不是你应该真正依赖的东西)。

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). 因此,您需要使用在模拟器上运行的模拟应用程序,并使用Core Data创建数据库(根据EliBud的回答)。 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 (ie copying a file is easy, importing data is hard), you're essentially using the Simulator as an administration tool. 虽然在“真实”计算机上执行此操作而不是在移动设备上执行此操作可能是有意义的(即复制文件很容易,导入数据很困难),您实际上使用模拟器作为管理工具。

But hey, if it works... 但是,嘿,如果有效......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM