Question

I'm using a category on NSManagedObject, called NSManagedObject+Serialization.h located here https://gist.github.com/nuthatch/5607405.

Everything pretty much works great, but I need to implement this but not sure how? I want to skip some objects.

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

}

How do I add object relationships to be skipped?

Many Thanks

Was it helpful?

Solution

In your managed object subclass you must implement the 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;
}

However, the implementation of the serialisation looks buggy (lines 80 and 93) ... (if you don't supply all objects to skip in advance)

The toDictionary of the relatedObject is skipped and so objects that the related objects may want to skip will not be added to the traversal history set ...

A quick fix might be to replace these lines with the full implementation of toDictionary and merging the traversal history set and the objectsToSkip sets returned ...

A better solution will be to change the signature of the toDictionary method to accept the traversal history and do the set merging there and replace the above lines with toDictionary of the related object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top