ParseQueryAdapter 键匹配为空 Android

ParseQueryAdapter key match empty Android

我有三个解析子类:Recipe、Ingredient 和 RecipeIngredient。 RecipeIngredient 有一个指向 Recipe 的指针和一个指向 Ingredient 的指针。

当我尝试创建一个 QueryFactory 来获取食谱的所有成分时。我正在尝试使用 whereMatchesKeyInQuery 执行此操作,但 objectId 不匹配。从文档看来,这应该是合法的。我错过了什么?

 public MeatIngredientListAdapter(Context context, final String recipeName) {
    super(context, new ParseQueryAdapter.QueryFactory<Ingredient>() {
        public ParseQuery<Ingredient> create() {

            ParseQuery<Ingredient> query = ParseQuery.getQuery(Ingredient.class);
            query.whereEqualTo("isMeatOrFat", true);

            ParseQuery<RecipeIngredient> riQuery = ParseQuery.getQuery(RecipeIngredient.class);
            riQuery.whereEqualTo("recipeName", recipeName);
            riQuery.include("ingredient");
            riQuery.whereEqualTo("isMeatOrFat", true);

            query.whereMatchesKeyInQuery("objectId", "ingredient.objectId", riQuery);

            return query;
        }
    });

}

在您的情况下,使用 whereMatchesKeyInQuery 是多余的。我可能没有足够的信息来调用你的应用程序,但如果你只创建一个 RelationIngredient,你似乎可以完全消除对 RecipeIngredient 的需求] class里面的Recipeclass。这将简化您的查询并使您的应用程序更具可扩展性并为您提供功能(如下所述)。如果你有这样的数据结构:

Recipe Class
 - Name (String)
 - ingredients (Relation of the Ingredient class)

Ingredient Class
 - <Columns to describe the ingredient that you already have in place>

现在您可以存储一个食谱 "points"(使用关系)到许多成分。

因此,示例条目可能如下所示:

Recipe
 Name
  PB&J
 ingredients
  Peanut Butter //this is a relation to the Peanut Butter Ingredient object
  Jelly         //this is a relation to the Jelly Ingredient object

Ingredient
 Name
  Peanut Butter
 Calories
  ...
 Cost
  ...

在代码中,我们将数据添加到 classes:

ParseObject ingredient1 = new ParseObject(Ingredient.class);
ingredient1.put("Name", "Peanut Butter");

ParseObject ingredient2 = new ParseObject(Ingredient.class);
ingredient1.put("Name", "Jelly");


ParseObject recipe = new ParseObject("Recipe");
recipe.put("Name", "PB&J");

ParseRelation<ParseObject> relation = recipe.getRelation("ingredients");
relation.add(ingredient1);
relation.add(ingredient2);

recipe.saveInBackground();

此设置背后的神奇之处在于,我们现在可以按名称指定食谱并获取所需的所有成分,但我们还可以检索其中包含特定成分的所有食谱(这是多对多关系),最重要的是它简化了您的查询。

现在使用此新设置进行您想要的原始查询:

ParseObject recipe = ...; // "PB&J" Recipe object.

ParseRelation relation = recipe.getRelation("ingredients");

// generate a query based on that relation
ParseQuery query = relation.getQuery();

query 将在执行查询时保存 recipe 对象的所有成分。

现在假设您想创建一个查询,从中获取包含特定成分的所有食谱:

ParseObject ingredient = ...

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

query.whereEqualTo("ingredients", ingredient); //use whereContainedIn for multiple ingredients

query 将在执行查询时包含在其 ingredients 关系列中具有指定成分的所有 Recipe 对象。

希望对您有所帮助。如果我严重误解了你的应用程序的结构,请告诉我 - 如果是这样,如果你给我新信息,我会修改我的答案,但老实说,我认为 "middle man" RecipeIngredient 迫使你使你的应用程序复杂化。