2015-05-19 30 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

這是光滑的!但是現在我試圖將配方中的步驟添加爲另一個關係,並且保存不起作用。你堅持每個對象只有一個關係? –