parse.com android 关系查询

parse.com android relationship query

我正在使用 Parse.com,我有 2 个 table:食谱和配料, 现在在 1 个食谱中我有很多配料 在解析中,我在成分 table 中使用父级连接了 table,我正在尝试构建 Recige 的 ArrayList(每个食谱包含成分的 ArrayList)

像这样:

 ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipe");

        query.orderByDescending("createdAt");
        query.include("Ingredient");

        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> RecipeList, ParseException e) {

//What should i write here ???
------------------------------

}

非常感谢大家

成分table: ObjectID ,NAME ,UNIT ,parent

由于配料只属于一个食谱列表,我假设食谱引用了属于其列表的配料。

如果您的参考是指向食谱中所有成分的指针,那么这就足够了

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipies");
    query.include("ingredients"); //Name of your ingredients table
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for(int index = 0; index < objects.size(); i++){
                    objects.get(i); //This is a recipe
                    objects.get(i).get("name of the column that hold the pointer"); //This is your ingredient
                }
            } else {
                Log.e("", e.getMessage());
            }
            functionCallback.done(objects, e);
        }

    });

可以找到有关 'include' 查询的更多信息 here(在关系查询下)

如果您将引用存储在一个数组中,您可以尝试做这样的事情

    ParseQuery<ParseObject> ingredientQuery = ParseQuery.getQuery("ingredients");

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipies");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for(int index = 0; index < objects.size(); i++){
                    objects.get(i); //This is a recipe
                    String[] pointers = objects.get(i).get("name of the column that hold the pointer"); 
                    //This is your ingredient reference (if you did 'recipe.put("ingredients", ingredient);', 
                    //the reference that would be saved is the id of the ingredient.
                    ingredientsQuery.whereContainedIn("objectId", pointers);
                    ingredientsQuery.findInBackground(new FindCallback<ParseObject>() {

                    @Override
                    public void done(List<ParseObject> ingredients, ParseException e) {
                    if (e == null) {
                        for(int index = 0; index < ingredients.size(); i++){
                            ingredients.get(i); //This is a ingredient
                        }
                    } else { 
                        Log.e("", e.getMessage());
                    }
               }
            } else { 
                Log.e("", e.getMessage());
            }
            functionCallback.done(objects, e);
        }

    });

觉得这应该就可以了!