序列化NSManagedObject

时间:2014-03-03 11:21:38

标签: core-data nsmanagedobject

我在NSManagedObject上使用了一个类别,名为NSManagedObject + Serialization.h,位于https://gist.github.com/nuthatch/5607405

一切都很有效,但我需要实现这个但不确定如何?我想跳过一些对象。

- (NSDictionary*) toDictionary {
// Check to see there are any objects that should be skipped in the traversal.
// This method can be optionally implemented by NSManagedObject subclasses.
NSMutableSet *traversedObjects = nil;
if ([self respondsToSelector:@selector(serializationObjectsToSkip)]) {
    traversedObjects = [self performSelector:@selector(serializationObjectsToSkip)];
}
return [self toDictionaryWithTraversalHistory:traversedObjects];

}

如何添加要跳过的对象关系?

非常感谢

1 个答案:

答案 0 :(得分:0)

在您的托管对象子类中,您必须实现serializationObjectsToSkip

- (NSMutableSet*) serializationObjectsToSkip
{
    NSMutableSet* objectsToSkip = [NSMutableSet new];

    //Here you select objects that relate to this object and you don't want to serialise.
    //Insert them into `objectsToSkip`

    return objectsToSkip;
}

然而,序列化的实现看起来有些错误(第80和93行)......(如果你没有提供所有要提前跳过的对象)

跳过toDictionary的{​​{1}},因此相关对象可能要跳过的对象不会被添加到遍历历史记录集中...

快速解决方法可能是将这些行替换为relatedObject的完整实现,并合并遍历历史记录集和返回的toDictionary集合...

更好的解决方案是更改objectsToSkip方法的签名以接受遍历历史记录并在那里进行集合合并并将上述行替换为相关对象的toDictionary