Question

My question is - how deep does Array data get retrieved with the .include() query? (this is for android on the parse.com platform)

The background - I am trying to decide the best way to design my data relations. I have X levels of relationships between my data such as: Grandparent object includes an array of Parent objects includes an array of Child object includes an array of Grandchild objects an array of Great-Grandchild objects or simply...

Grandparent object -> Parent objects -> Child objects -> Grandchild objects -> Great-Grandchild objects.

If I perform a query on the Grandparent object and want to include the array of Parent objects then I know that I have to add the ".include(Parent)" in the query. But, if I do this, how deep will the array inclusion go? Will it also include the array of Child objects (and so on) down to the array of Great-grandchildren objects?

I only want the Grandparent object and its Parent objects (and not all the other array children objects). I don't need all the child(ren) data arrays - just the most immediate child array. But it seems to make sense to have each level to have an array of it's children (since the amount of children in each array would be about 10 to 50 in my application).

Will the .include() only go one level deep or go further and is there a way to control the depth?

Here's what I have tried:

//create grandchildren
ParseObject pGrandChild1 = new ParseObject("Grandchild");
pGrandChild1.put("name", "Child 1");
pGrandChild1.saveInBackground();
ParseObject pGrandChild2 = new ParseObject("Grandchild");
pGrandChild2.put("name", "Child 2");
pGrandChild2.saveInBackground();
//create children
ParseObject pChild1 = new ParseObject("Child");
pChild1.put("name", "dad");
ArrayList<ParseObject> pGrandchildArray = new ArrayList<ParseObject> ();
pGrandchildArray.add(pGrandChild1);
pGrandchildArray.add(pGrandChild2);
pChild1.put("grandchildren", pGrandchildArray);
pChild1.saveInBackground();

With the code above, it only adds the 2 grandchildren to parse and not the child (which is the parent or dad of the 2 children). If I comment out the 4 lines related to the ArrayList code then it adds the pChild1 (the "dad" object) to parse as well as his 2 kids. Obviously this does not add the Array relationship like I want but, I'm not sure why the code above does not add the relationship.

I even added a Thread.sleep(5000); after the pGrandChild2.saveInBackground(); line thinking that maybe a little time in between saving the objects was needed. That did not help either.

No correct solution

OTHER TIPS

As per the documentation:

You can also do multi level includes using dot notation. If you wanted to include the post for a comment and the post's author as well you can do:

query.include("post.author");

You can issue a query with multiple fields included by calling include multiple times. This functionality also works with ParseQuery helpers like getFirst() and getInBackground().

In your case that means you could query the Grandparent object with:

query.include("parents.children.grandchildren.greatgrandchildren");

Just note that parents is the property name of the array on Grandparent, children is the array property on Parent, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top