parse.com - 使用.include()查询检索数组数据有多深?

时间:2022-09-07 10:11:20

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

我的问题是 - 使用.include()查询检索数组数据有多深? (这是针对parse.com平台上的android)

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...

背景 - 我正在尝试决定设计数据关系的最佳方法。我的数据之间有X级关系,例如:祖父对象包括一个Parent对象数组,包括一个Child对象数组,包括一个Grandchild对象数组,一个Great-Grandchild对象数组或简单地...

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?

如果我对祖父对象执行查询并想要包含父对象数组,那么我知道我必须在查询中添加“.include(Parent)”。但是,如果我这样做,数组包含有多深?它是否还包括子对象(等等)的数组,直到孙子对象的数组?

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).

我只想要祖父对象及其父对象(而不是所有其他数组子对象)。我不需要所有子(ren)数据数组 - 只是最直接的子数组。但是让每个级别都有一个孩子的数组似乎是有道理的(因为在我的应用程序中每个数组中的子项数量大约是10到50)。

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

.include()只会深入一级还是更深入,是否有办法控制深度?

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.

使用上面的代码,它只会添加2个孙子来解析而不是孩子(这是2个孩子的父母或父亲)。如果我注释掉与ArrayList代码相关的4行,那么它会添加pChild1(“dad”对象)来解析他的2个孩子。显然这不会像我想要的那样添加数组关系,但是,我不确定为什么上面的代码不会添加关系。

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.

我甚至添加了一个Thread.sleep(5000);在pGrandChild2.saveInBackground()之后;认为在保存对象之间可能需要一点时间。这也没有帮助。

1 个解决方案

#1


1  

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().

您可以通过多次调用include来发出包含多个字段的查询。此功能也适用于ParseQuery帮助程序,如getFirst()和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.

请注意,parent是Grandparent上数组的属性名,children是Parent上的数组属性,等等。

#1


1  

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().

您可以通过多次调用include来发出包含多个字段的查询。此功能也适用于ParseQuery帮助程序,如getFirst()和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.

请注意,parent是Grandparent上数组的属性名,children是Parent上的数组属性,等等。