Parse-NSCoding icon indicating copy to clipboard operation
Parse-NSCoding copied to clipboard

Encoding non parse properties causes a problem

Open npahucki opened this issue 11 years ago • 1 comments

Hi, great job on this and the caching framework. I have a problem however that I need some explanation about. Why is is that non-parse properties are serialized? This doesn't seem right (at least for the purpose of caching subclasses of PFObject). The problem is this, if you register your own sub class of PFObject, and it has some read-only properties (which are usually calculated) these may cause problems.

Assume class A and class B, both subclasses of PFObject. A refers to B, B has a read only property that calculates a value based on a PFObject field. However, when A was loaded, it only loads a pointer to B, and not all of B's fields. When the PFObject+NSCoding tries to encode A, it transitively encodes B, including B's non Parse fields. This means that the readonly property method is called, which tries to read a field that was not loaded and fails with Parse library saying that the field needs to be loaded first.

I worked around this by commenting out the following two lines of PFObject_NSCoding (line 87):

    //Deserialize all non-Parse properties
    NSDictionary* nonParseProperties = [self nonDynamicProperties];
    [self decodeProperties:nonParseProperties withCoder:aDecoder];

It seems that we need a way to turn off serialization of Non-Parse fields, or indicate a property (especially read-only ones) should not be serialized. I'm happy to work on a solution for this, but first I wanted to fully understand the motivation to start with for serializing non-parse fields. My best guess is that PFObject+NSCoding is designed to be a generic serialization mechanism. In all cases, it doesn't make sense to serialize read-only properties since trying to restore them later could only fail.

Please let me know your thoughts.

npahucki avatar Jun 27 '14 20:06 npahucki

Ok, I think I found the 'right' way to fix this, pull request forthcoming.

NSObject+Properties (around line 33 - Bold part added):

- (NSDictionary *)nonDynamicProperties {
    NSMutableDictionary* output = [[NSMutableDictionary alloc] init];
    NSDictionary* properties = [self properties];
    for (NSString* key in properties) {
        NSArray* attributes = properties[key][@"attributes"];
        if (![attributes containsObject:@"D"] && **![attributes containsObject:@"R"]**) {
            output[key] = properties[key];
        }
    }
    return output;
}

If you see any problems with this, let me know.

npahucki avatar Jun 27 '14 21:06 npahucki