导入数据的核心数据最佳实践
我有一个应用程序(iPad 和 Mac Lion),它使用 CoreData 进行存储。该应用程序从多个硬件设备导入数据,将数据解码为多个离散事件,并向用户呈现事件列表,然后用户可以选择保存哪些事件。
目前,我为每个事件创建一个非 CoreData 对象,然后将用户选择的事件转换为 CoreData 实体以保存在商店中。这意味着我基本上为每个对象都有两个类;一个托管 CoreData 对象,以及另一个表示用户选择事件之前的事件的非托管对象。
我怀疑必须有一种更好、更低的代码开销(读:更易于管理)的方法来做到这一点:
- 我可以创建核心数据实体而不将它们保存到存储中,然后只保存用户选择的实体吗?
- 或者有其他方法可以使用 CoreData 来构建它吗?
I've got an application (iPad & Mac Lion) which uses CoreData for its storage. The app imports data from a number of hardware devices, decodes the data into a number of discrete events, and presents the list of events to the user, who then has the choice to select which events are saved.
At the moment, I create a non-CoreData object for each event, and then convert the ones selected by the user to CoreData entities to be saved in the store. This means I've basically got two classes for each object; one managed CoreData object, and another non-managed object that represents the event before the user selects it.
I suspect there must be a better, lower code-overhead, (read: more manageable) way to do this:
- Can I create core data entities without having them saved into the store, and then only save the ones selected by the user?
- Or is there some other way to structure this, using CoreData?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是托管对象上下文的用途。 :-) 创建一个与显示用户“数据库”中实际数据的普通数据上下文分开的“导入上下文”。在该上下文中创建所有对象(并且不保存),让用户删除他/她不想要的对象,然后在完成导入和保存后合并上下文。
This is what Managed Object Contexts are for. :-) Create an "import context" separate from the normal data context that displays the actual data the user has in their "database". Create all the objects (and don't save) in that context, let the user drop the ones he/she doesn't want, then merge the contexts when done with import and save.