2014-03-03 13 views
0

我在NSManagedObject上使用一個類別,名爲NSManagedObject + Serialization.h,位於這裏https://gist.github.com/nuthatch/5607405序列化NSManagedObject

一切都非常好,但我需要實現這一點,但不知道如何?我想跳過一些對象。

- (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]; 

}

如何添加對象關係要跳過?

非常感謝

回答

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)...(如果你不提供所有需要提前跳過的對象)

relatedObjecttoDictionary被跳過,因此相關對象可能想跳過的對象wi LL不被添加到穿越歷史的集...

速戰速決可能是全面實施的toDictionary和合並的遍歷的歷史設定,並返回objectsToSkip套更換這些線路...

更好的解決方案是改變toDictionary方法的簽名以接受遍歷歷史記錄,並在那裏進行集合合併用相關對象的toDictionary替換上述行。

+0

謝謝!完美地工作! – ourmanflint