2015-05-19 50 views
0

我有三个解析子类:Recipe,Ingredient和RecipeIngredient。 RecipeIngredient有一个指向食谱的指针和一个指向成分的指针。ParseQueryAdapter键匹配为空Android

当我试图创建一个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; 
     } 
    }); 

} 
+0

你能解释子查询的目的吗?我不清楚食谱配料应该做什么,以返回成分 – cYrixmorten

回答

1

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

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

query将持有的所有成分为recipe对象。

现在假设你想创建一个查询,你得到所有含有一定成分的食谱:

ParseObject ingredient = ... 

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

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

query将包含有特定的成分在他们ingredients关系列的所有配方对象时,查询被执行。

我希望这有助于你。如果我严重误解了你的应用程序的结构,请让我知道 - 如果是的话,我会修改我的答案,如果你给我新的信息,但老实说,我认为“中间人”RecipeIngredient迫使你复杂化你的应用程序。

+0

感谢你,@MaxWorg。我一直在试图找出构建数据的最佳方式,并让适配器工作。我很难弄清楚如何将东西放入子类中。有时候,这些文档示例对我来说太简单了,无法真正理解做某件事的最佳方式。 –

+0

我已经感觉到你的痛苦了:D我认为文档是相当不完整的(在某些情况下,我发现文档和例子已经过时了,这使得它们不正确),我一个人留下来想办法解决很多问题倍。让我知道我是否可以得到更多帮助。 –

+0

这是光滑的!但是现在我试图将配方中的步骤添加为另一个关系,并且保存不起作用。你坚持每个对象只有一个关系? –