0

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

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

 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;
        }
    });

}
4

1 回答 1

1

在你的情况下,使用whereMatchesKeyInQuery是矫枉过正的。我可能没有足够的信息来调用你的应用程序,但如果你只是在类中RecipeIngredient创建一个类,你似乎可以减少对所有的需求。这将简化您的查询并使您的应用程序更具可扩展性并为您提供功能(如下所述)。如果你有这样的数据结构:RelationIngredientRecipe

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

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

现在,您可以存储一个“指向”(使用关系)到许多成分的配方。

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

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

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

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

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

现在假设您要创建一个查询,在其中获取包含某种成分的所有食谱:

ParseObject ingredient = ...

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

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

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

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

于 2015-05-19T20:50:56.943 回答