Storing a PFObject pointer in an app's PFConfig

266 views Asked by At

I want to store a pointer to a specific object as an Object Pointer in a PFConfig, similar to how objects can point to each other in the database. I see there is a way to create an Object field in the PFConfig, but when I enter the corresponding objectID it says "invalid value for Object Type". How do I store object links in the PFConfig?

2

There are 2 answers

1
rickerbh On BEST ANSWER

The Object field type in PFConfig in looks like it only stores JSON objects (converted to NSDictionary with the iOS API), not relations to other Parse objects. And it seems to do some sort of validation, so even if you enter {"__type":"Pointer", "className":"MyClass", "objectId":"1234xyz"} (which is valid JSON), the save fails validation with a __type field is not allowed. error.

Doesn't look like it can be done with native functionality sorry. You could store your own pointer style object (i.e., just store the object ID, or a {"type": "pointer", "objectId": "ABC123"} style reference), but you'd be responsible for managing the relationship, and retrieving the downstream object.

You could also try converting the object you want to save to a JSON representation, but if you wanted to use it as a real PFObject you'd need to reconstruct the object based on the data in the PFConfig, and it's probably safer just to load a real PFObject based on a reference if the ability to update that object is important to your config.

1
danh On

Expanding on my suggestion under @rickerbh's excellent answer, consider saving something that indicates the desired object, rather than pointing to it. This way it can be found in a query, even if the object's objectId changes.

For example, you could add a config parameter named myConfigParam equal to { "class":"MyClass", "uniqueField":"foo" }, where one or more uniqueField attributes fully describe the object. Then, the config'd object can be picked up this way, in objective-c...

- (void)fetchConfigObjectWithCompletion:(void (^)(PFObject *, NSError *))completion {
    [PFConfig getConfigInBackgroundWithBlock:^(PFConfig *config, NSError *error) {
        if (!error) {
            NSString *className = config[@"myConfigParam"][@"class"];
            NSString *uniqueField = config[@"myConfigParam"][@"uniqueField"];
            PFQuery *query = [PFQuery queryWithClassName:className];
            [query whereKey:@"uniqueField" equalTo: uniqueField];
            [query getFirstObjectInBackgroundWithBlock:completion];
        } else {
            completion(nil, error);
        }
    }];
}

Since the config object is rendered as an NSDictionary, you could even iterate its keys, and add equalTo constraints to the query in a loop without knowing on the client what those keys are.