Reputation: 621
Let's say I have a class Parent that has a one-to-many relation with a target class Child. Both Parent and Child objects are not user created, I am creating this objects in Data Browser and I don't know how to set a relationship or a pointer on a Child back to it's particular Parent. I can get Child objects of a Parent with no problem. But, how can I get the Parent of a Child? I don't want to keep passing both Parent and Child objects from VC to VC. Once again both objects are NOT user created so I can't do:
[Parent setObject:Child forKey:"child"];
and then query Parent class like this:
[query whereKey:@"child" isEqualTo:Child];
Also querying all Parents and their relations and then getting that particular Child and then going back to the Parent seem to be an overkill.
So, how can I set the particular Parent on a Child object in Data Browser to point back to the parent? And how to get that Parent object from the code if I want to use only Child object?
Upvotes: 1
Views: 809
Reputation: 16874
This is a case of where using a reverse pointer might be more useful. This can be done in addition to your existing relationship if you want, but requires some fancier save logic.
Simply add a parent
column to Child
that is of type Pointer<Parent>
.
To get all children of a parent you just do a query:
[childQuery whereKey:@"parent" isEqualTo:parent];
To query a child and also get the parent:
[childQuery includeKey:@"parent"];
// filter as needed
In the block that looks are children you can just use:
PFObject *parent = child[@"parent"];
It will be a fully populated Parent
due to the includeKey:
.
NOTE: if you keep both relationships, creating new objects gets trickier as you'll have to save the parent/child (with child.parent
unset) then update the child to point to the parent and save it again.
Upvotes: 0