0

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
  • You never save the relation... Your code's kind of reversed in a sense... I'll type up an answer to explain. Commented Dec 7, 2014 at 0:37

1 Answer 1

3

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;
}];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for pointing out my dumb mistake! I've just added the line [trip saveInBackground] inside the block, so I do not have to do the query for trip synchronous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.