I am trying to get my head around PFRelation in parse. I have a class called "girlBio" that stores information about girls and a class called "stuff" that stores information about items. code below:
PFObject *item = [PFObject objectWithClassName:@"stuff"];
item[@"Name"] = @"PS3";
PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
girl[@"Name"] = @"Jessica";
PFObject *girl2 = [PFObject objectWithClassName:@"girlBio"];
girl2[@"Name"] = @"Cindy";
PFRelation *relation = [item relationForKey:@"owners"];
[relation addObject:girl];
[relation addObject:girl2];
[item saveInBackground];
--------------------------------- update also tried this -------------------------
PFObject *item = [PFObject objectWithClassName:@"stuff"];
item[@"Name"] = @"PS3";
PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
girl[@"Name"] = @"Jessica";
[item saveInBackground];
[girl saveInBackground];
PFRelation *relation = [item relationForKey:@"owners"];
[relation addObject:girl];
[item saveInBackground];
So I want this item to be owned by several girls however when I run the program I get this error:
Error: can't add a non-pointer to a relation (Code: 111, Version: 1.6.0)
Can someone help please?
Thank you
You need to save your objects girl1
and girl2
before saving the relationship. Otherwise, even thought your local copy knows about them, the server does not.
UPDATE
You also need to make sure the saves for girl1
and girl2
and item
complete before you save the relation. However, you probably don't want to run these saves on the primary thread, so I'd recommend something like this (which I just ran without issue):
dispatch_async(dispatch_get_main_queue(), ^{
PFObject *item = [PFObject objectWithClassName:@"stuff"];
item[@"Name"] = @"PS3";
PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
girl[@"Name"] = @"Jessica";
[item save];
[girl save];
PFRelation *relation = [item relationForKey:@"owners"];
[relation addObject:girl];
[item saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//Do something after the last save...
}];
});