核心数据 - 添加静态/查找数据
我正在创建一个应用程序,并使用 Core Data 作为我的后端数据源。
我想知道如何手动将条目添加到核心数据实体中,该实体可用于保存在整个应用程序生命周期中永远不会改变的静态数据。
I am creating an application, and am using Core Data as my backend data source.
I am wondering how I can manually add entries into a Core Data Entity which can be used to hold static data which will never change throughout the applications lifetime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题有很多解决方案。首先,对于数据输入,您可以在几分钟内创建一个简单的桌面应用程序,以便您进行数据输入。由于底层的 sqlite 文件是相同的,因此您可以将该文件复制到您的项目中。
至于访问它,取决于你的数据结构。如果您有静态且可编辑的实体(例如可以添加的查找列表),那么我建议在捆绑包中携带 sqlite 文件,并在首次启动时将其复制到文档目录。
如果表永远不可写,那么您可以将静态数据保留在包中,并在可写的文档目录中创建第二个存储。然后,您可以在创建核心数据堆栈时将两者捆绑在一起,应用程序的其余部分将把它们视为单个数据库/对象图。唯一的问题是您无法建立跨文件的关系。
There are many solutions to this issue. First, for data entry, you can create a trivial desktop application in a couple of minutes that will let you do the data input. Since the underlying sqlite file is the same, you can then copy the file to your project.
As for accessing it, depends on your data structure. If you have entities that are both static and editable (such as a look up list that can be added to), then I suggest carrying the sqlite file in your bundle and on first launch copy it to the documents directory.
If the tables are NEVER writable then you can leave the static data in your bundle and create a second store in the documents directory that is writable. You can then bundle the two together on creation of your Core Data stack and the rest of your application will treat them as if they are a single database/object graph. The only issue with this is that you cannot have relationships across files.
我从未尝试过这个,但是我认为您可以使用在模拟器中运行的示例程序来填充核心数据,该程序用数据填充数据库。
然后你可以从模拟器中获取 .sqlite 数据库并放入 Xcode 资源文件夹中。
每次您的应用程序启动时,即使是第一次运行数据库也会包含您在示例程序中首次填充的对象。
I have never tried this, however I think you can populate core data with an example program which you run in the simulator which populates the database with the data.
Then you can get .sqlite database out of the simulator and into Xcode resource folder.
Every time your app starts, even first run the database will contain the objects you first populated it with in the example program.