我可以编码 NSManagedObject 的子类吗? [英] Can I encode a subclass of NSManagedObject?

查看:18
本文介绍了我可以编码 NSManagedObject 的子类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对 iPhone 应用程序的,但我认为这并不重要.我需要使用 iPhone 的 GameKit 通过蓝牙发送一个自定义对象(由 Core Data 管理).通常,我只会使用 NSKeyedArchiver 将对象打包为数据对象并将其发送到线路上,然后取消归档对象,我就完成了.当然,我还需要在我的自定义对象中实现 initWithCoder: 和 encodeWithCoder: 方法.

This is for an iPhone App, but I don't think that really matters. I need to send a custom object (which is managed by Core Data) over bluetooth using the iPhone's GameKit. Normally, I would just use an NSKeyedArchiver to package up the object as a data object and ship it over the line, then unarchive the object and I'm done. Of course, I would need to implement the initWithCoder: and encodeWithCoder: methods in my custom object as well.

我不确定这是否可以通过由 Core Data 管理的 NSManagedObject 类来完成.他们会一起玩吗?我猜一旦我将编码的托管对象发送到另一个设备并取消编码,我就会将此接收到的对象添加到另一个设备的上下文中.这个对吗?我是否遗漏了任何步骤?

I'm not sure if this can be done with an NSManagedObject class, which is managed by Core Data or not. Will they play nice together? I'm guessing once I ship the encoded managed object over to the other device and unencode it, I would just add this received object to the other device's context. Is this correct? Am I missing any steps?

推荐答案

NSManagedObject 实例不能有意义地存在于 NSManagedObjectContext 实例之外,所以我不会费心尝试在两个上下文之间直接序列化和反序列化 NSManagedObject 所需的 NSCoding 舞蹈(您可以这样做;见下文).相反,我会创建一个具有适当属性键/值的字典(您可以通过 instance.entity.attributesByName.allKeys 通过托管对象实例的属性名称获取属性名称(您可以使用 [实例 dictionaryWithValuesForKeys:keys] 获取属性:值对的字典).我会将关系信息作为 NSURL-encoded NSManagedObjectIDs 发送.不要忘记在字典中包含实例managedObjectID(作为NSURL),以便您可以重新连接任何关系另一端的对象.您必须为您正在编码的实例的任何关系目标递归创建这些字典.

An NSManagedObject instance can't meaningfully exist outside of an NSManagedObjectContext instance, so I wouldn't bother trying to do the NSCoding dances required to directly serialize and deserialize an NSManagedObject between two contexts (you can do this; see below). Instead I would create a dictionary with the appropriate attribute key/values (you can get the attribute names via the managed object instance's attribute names via instance.entity.attributesByName.allKeys (you can use [instance dictionaryWithValuesForKeys:keys] to get the dictionary of attribute:value pairs) . I would send relationship information as NSURL-encoded NSManagedObjectIDs. Don't forget to include the instance managedObjectID (as an NSURL) in the dictionary so that you can reconnect any relationships to the object on the other end. You'll have to recursively create these dictionaries for any targets of relationships for the instance you're encoding.

然后通过线路发送 dict(s) 并在另一端将它们重组为新托管对象上下文中的实例(您可以使用 setValuesForKeysWithDictionary:).

Then send the dict(s) across the wire and reconstitute them on the other end as instances in a new managed object context (you can use setValuesForKeysWithDictionary:).

您可能会注意到这正是 NSCoder 系统将为您做的事情,除了您必须使用 classForCoderreplacementObjectForCoder:awakeAfterUsingCoder: 以及一个自定义的 NSDictionary 子类来处理所有的 NSManageObject-to-NSDictionary 映射和签证反之.根据我的经验,这段代码比它的价值更麻烦,除非您有一个要序列化的复杂/深层对象图.对于没有关系的单个 NSManagedObject 实例,将其转换为 dict 并返回自己肯定更容易.

You may notice that this is exactly what the NSCoder system would do for you, except you would have to use the classForCoder, replacementObjectForCoder: and awakeAfterUsingCoder: along with a custom NSDictionary subclass to handle all the NSManageObject-to-NSDictionary mapping and visa versa. This code is more trouble than it's worth, in my experience, unless you have a complex/deep object graph that you're trying to serialize. For a single NSManagedObject instance with no relationships, it's definitely easier to just do the conversion to a dict and back yourself.

这篇关于我可以编码 NSManagedObject 的子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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