I have a bunch of data that I want to store in a Core Data file. The problem is that It only gets created at runtime. How can I create it before, save it into my bundle, and then display it to the user?
我有一堆数据要存储在Core Data文件中。问题是它只在运行时创建。如何创建它,将其保存到我的包中,然后将其显示给用户?
2 个解决方案
#1
I do this by running my app (or a utility written just for this purpose) in the simulator during development and populating it with its pre-set information. I then find the file on disk, put it in my project, and make sure it is included in the "Copy files" build phase.
我通过在开发期间在模拟器中运行我的应用程序(或为此目的编写的实用程序)并使用其预设信息填充它来完成此操作。然后我在磁盘上找到该文件,将其放入我的项目中,并确保它包含在“复制文件”构建阶段。
At start up, I look for the file in the app bundle, and copy it to where CoreData expects it...that is, some writable location.
在启动时,我在应用程序包中查找该文件,并将其复制到CoreData期望的位置...即一些可写位置。
#2
My preference is to use a property list (.plist) file that contains arrays and dictionaries of the "default data" I wish to preload, then on first run load that default data in.
我的偏好是使用属性列表(.plist)文件,该文件包含我希望预加载的“默认数据”的数组和字典,然后在第一次运行时加载默认数据。
I use code in my core data stack to check the plist file against existing default data and, in the case that I have updated the plist file with some additional default data (for subsequent production release), this is added.
我在核心数据堆栈中使用代码来检查plist文件是否存在现有的默认数据,如果我已经使用一些额外的默认数据更新了plist文件(用于后续的生产版本),则会添加此代码。
I also use code in my core data stack to change or delete default data as necessary, but this is data specific.
我还在核心数据堆栈中使用代码来根据需要更改或删除默认数据,但这是特定于数据的。
#1
I do this by running my app (or a utility written just for this purpose) in the simulator during development and populating it with its pre-set information. I then find the file on disk, put it in my project, and make sure it is included in the "Copy files" build phase.
我通过在开发期间在模拟器中运行我的应用程序(或为此目的编写的实用程序)并使用其预设信息填充它来完成此操作。然后我在磁盘上找到该文件,将其放入我的项目中,并确保它包含在“复制文件”构建阶段。
At start up, I look for the file in the app bundle, and copy it to where CoreData expects it...that is, some writable location.
在启动时,我在应用程序包中查找该文件,并将其复制到CoreData期望的位置...即一些可写位置。
#2
My preference is to use a property list (.plist) file that contains arrays and dictionaries of the "default data" I wish to preload, then on first run load that default data in.
我的偏好是使用属性列表(.plist)文件,该文件包含我希望预加载的“默认数据”的数组和字典,然后在第一次运行时加载默认数据。
I use code in my core data stack to check the plist file against existing default data and, in the case that I have updated the plist file with some additional default data (for subsequent production release), this is added.
我在核心数据堆栈中使用代码来检查plist文件是否存在现有的默认数据,如果我已经使用一些额外的默认数据更新了plist文件(用于后续的生产版本),则会添加此代码。
I also use code in my core data stack to change or delete default data as necessary, but this is data specific.
我还在核心数据堆栈中使用代码来根据需要更改或删除默认数据,但这是特定于数据的。