我可以手动创建Core Data Entity类的实例以进行临时使用吗? [英] Can I create manually an instance of an Core Data Entity class for temporary usage?

查看:129
本文介绍了我可以手动创建Core Data Entity类的实例以进行临时使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为朋友的核心数据实体的自定义类。在解析XML文件时,我需要创建临时实例来保存临时数据,而不使用Core Data。所以有两个选项:



A)创建一个NSMutableDictionary来保存临时数据,同时从XML解析一个对象。



B)使用实体的类在解析时存储临时数据。我更喜欢,因为它更清晰,更干燥,对我来说,因为数据结构已经在那里声明,我不必在NSMutableDictionary中复制该实体的整个数据模型。



但是B有一个问题:默认情况下,所有的属性都是@dynamic,就我所知,Core Data负责在运行时创建实现。所以我不能只使用那里的属性。因此,这提出了一个问题,如果它值得努力,甚至可能修改该类,以便可以使用没有Core Data作为临时数据容器对象,即通过创建实例变量。当然,如果我不得不创建一个字典来保存临时数据,这将没有任何意义,我只是去A。

解决方案

更新:听起来你可以使用nil上下文(见下面的亚当回答)

实例化管理对象子类的唯一方法是将它插入上下文。



使用可变字典(NSMutableDictionary)是最好的方法,因为你可以使用 setValuesForKeysWithDictionary:用一个方法调用来填充新的托管对象。



另一种方法是插入托管对象,然后如果您确定它不是新的,请删除它。如果没有保存,对象将永远不会被写入持久化存储。

注意:假设您在添加其唯一ID之前确定对象是否为新对象,或者排除临时对象(带有属性或使用它们的集合过滤),以获取唯一ID,以便您只检查之前存在的对象。


I have an custom class for an Core Data entity, called 'Friends'. As I am parsing an XML File, I need to create temporary instances to hold temporary data, without using Core Data at this point. So there are two options:

A) create an NSMutableDictionary to hold the temporary data while parsing an "object" from the XML.

B) use the class of the entity to store temporary data while parsing. I would prefer that, because it is more clear and more DRY for me, since the data structure is declared in there already and I wouldn't have to duplicate the whole data model of that entity in an NSMutableDictionary.

But B has a problem: By default, all the properties are @dynamic, and as far as I know, Core Data takes care of creating the implementations at runtime. So I can't just use the properties there. So this raises the question if it's worth the effort and even possible to modify that class in such a way that it can be used without Core Data as a temporary data container object, i.e. by creating instance variables. Of course, if I had to create an dictionary in there to hold the temporary data, that would make no sense at all, and I would just go with A.

解决方案

UPDATE: It sounds like you can use a nil context (see Adam's answer below)

Unfortunately, the only way to instantiate a managed object subclass is by inserting it into a context.

Using a mutable dictionary (NSMutableDictionary) is the best approach since you can use setValuesForKeysWithDictionary: to populate the new managed object with a single method call.

Another approach would be to insert the managed object and then delete it if you determine that it is not new. If you haven't saved, the object will never even have been written to the persistent store.
Note: this assumes that you determine if the object is new before adding its unique ID or you exclude the temporary objects (with an attribute or filtering out using a collection of them) when fetching the unique ID(s) so that you are only checking objects that existed previously.

这篇关于我可以手动创建Core Data Entity类的实例以进行临时使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆