Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding non parse properties causes a problem #16

Open
npahucki opened this issue Jun 27, 2014 · 1 comment
Open

Encoding non parse properties causes a problem #16

npahucki opened this issue Jun 27, 2014 · 1 comment

Comments

@npahucki
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant