【问题标题】:Serializing NSManagedObject序列化 NSManagedObject
【发布时间】:2014-03-03 11:21:38
【问题描述】:

我在 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];

}

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

非常感谢

【问题讨论】:

    标签: core-data nsmanagedobject


    【解决方案1】:

    在您的托管对象子类中,您必须实现 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 行)...(如果您没有提前提供所有要跳过的对象)

    relatedObject 中的toDictionary 被跳过,因此相关对象可能想要跳过的对象不会被添加到遍历历史集...

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2012-05-16
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多