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
?
Storing a PFObject pointer in an app's PFConfig
266 views Asked by Satre At
2
There are 2 answers
1
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.
The
Object
field type inPFConfig
in looks like it only stores JSON objects (converted toNSDictionary
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 thePFConfig
, and it's probably safer just to load a realPFObject
based on a reference if the ability to update that object is important to your config.