PFRelation addObject does not add relation

472 views Asked by At

I'm trying to add an object to a relation in Parse, although the code gets exectued without any errors the relation does not appear in the backend, therefore the object was not saved.

PFObject *newContact = [PFObject objectWithClassName:@"Contact"];

[newContact saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    PFQuery *query = [PFQuery queryWithClassName:@"Trip"];
    PFObject *trip = [query getObjectWithId:self.parseID];

    PFRelation *rel = [trip relationForKey:@"contacts"];
    [rel addObject:newContact];

    contact.parseID = newContact.objectId;
}];

I've also checked if the PFObject trip is correct and I get back the desired object with the corresponding id. Also the key contacts is double-checked and correct.

1

There are 1 answers

1
Lyndsey Scott On BEST ANSWER

The problem is that you never save the relation. You create the PFRelation within the block, add an object to it, and do nothing else with it... You never save it.

Instead, try fetching the trip object and creating the PFRelation outside of the save block, ex:

PFQuery *query = [PFQuery queryWithClassName:@"Trip"];
PFObject *trip = [query getObjectWithId:self.parseID];

PFObject *newContact = [PFObject objectWithClassName:@"Contact"];

PFRelation *rel = [trip relationForKey:@"contacts"];
[rel addObject:newContact];

[newContact saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    contact.parseID = newContact.objectId;
}];