ParseQueryAdapter钥匙匹配空的Andr​​oid [英] ParseQueryAdapter key match empty Android

查看:202
本文介绍了ParseQueryAdapter钥匙匹配空的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我试图创建一个QueryFactory能为配方的所有成分。我试图用whereMatchesKeyInQuery做到这一点,但的ObjectID不配套。从文档,看来这应该是合法的。我缺少什么?

 公共MeatIngredientListAdapter(上下文的背景下,最终字符串RecipeName中){
    超(背景下,新ParseQueryAdapter.QueryFactory<性成分GT;(){
        公共ParseQuery<性成分GT;创建() {            ParseQuery<性成分GT;查询= ParseQuery.getQuery(Ingredient.class);
            query.whereEqualTo(isMeatOrFat,真正的);            ParseQuery< RecipeIngredient> riQuery = ParseQuery.getQuery(RecipeIngredient.class);
            riQuery.whereEqualTo(RecipeName中,RecipeName中);
            riQuery.include(成分);
            riQuery.whereEqualTo(isMeatOrFat,真正的);            query.whereMatchesKeyInQuery(OBJECTID,ingredient.objectId,riQuery);            返回查询;
        }
    });}


解决方案

在您的情况下使用 whereMatchesKeyInQuery 是矫枉过正。我可能没有足够的信息来作出这一呼吁有关您的应用程序,但似乎你将能够削减了 RecipeIngredient 在一起,如果你只是创建一个<$需要C $ C>关联的成分食谱类的内部类。这将简化您的查询,并让您的应用更灵活,可为您提供的功能(下文解释)。如果你有一个数据结构是这样的:

 类食谱
  - 名称(字符串)
  - 成分(成分类的关系)成份类
  - &LT;列来形容,你已经到位&GT的成分;

现在,你可以存储一个食谱,点(使用关系)的许多成分。

所以一个示例项可能如下所示:

 配方
 名称
  PB&安培;Ĵ
 成分
  花生酱//这是对花生酱成分的对象的关系
  果冻//这是果冻成分对象的关系成分
 名称
  花生酱
 卡路里
  ...
 成本
  ...

和这里code,我们将数据添加到类:

 的parseObject ingredient1 =新的parseObject(Ingredient.class);
ingredient1.put(姓名,花生酱);的parseObject ingredient2 =新的parseObject(Ingredient.class);
ingredient1.put(姓名,果冻);
的parseObject配方=新的parseObject(配方);
recipe.put(姓名,PB&放大器; J);ParseRelation&LT;&的parseObject GT;关系= recipe.getRelation(配料);
relation.add(ingredient1);
relation.add(ingredient2);recipe.saveInBackground();

这种设定背后隐藏神奇的是,我们现在可以按名称指定配方,并得到所有成分,如你想,但我们也可以检索有一定的成分在其中所有的食谱(这是一个多对一的美丽一对多的关系)和关于它简化了您的查询之上。

现在你用这个新设置想要的原始查询:

 的parseObject配方= ...; //PB&放大器; J配方对象。ParseRelation关系= recipe.getRelation(配料);//生成基于关系的查询
ParseQuery查询= relation.getQuery();

查询将持有的所有成分为在执行查询配方对象。

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

 的parseObject成分= ...ParseQuery&LT;&的parseObject GT;查询= ParseQuery.getQuery(配方);query.whereEqualTo(配料,成分); //使用whereContainedIn的多种成分

查询将包含在他们的成份指定的成分时,执行查询有关列的所有配方对象

我希望这有助于你。请让我知道,如果我严重误解了您的应用程序的结构 - 如果是这样,如果你给我新的信息,但老实说,我认为我将修改我的回答了中间人 RecipeIngredient 强迫你您的应用程序变得更加复杂。

I have three parse subclasses: Recipe, Ingredient, and RecipeIngredient. RecipeIngredient has a pointer to a Recipe, and a pointer to an Ingredient.

When I am trying to create a QueryFactory to get all the ingredients for a recipe. I am trying to do this with whereMatchesKeyInQuery, but the objectIds aren't matching. From the docs, it appears that this should be legal. What am I missing?

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

}

解决方案

In your case the use of whereMatchesKeyInQuery is overkill. I might not have enough information to make this call about your app but is seems that you would be able to cut out the need for RecipeIngredient all together if you just create a Relation of the Ingredient class inside the Recipe class. This will simplify your queries and make your app more scalable and give you features (explained below). If you had a data structure like this:

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

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

Now you can store one recipe that "points" (using relations) to many ingredients.

So an example entry might look like this:

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

And here in code we add the data to the 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();

The magic behind this setup is that we can now specify a recipe by name and get all ingredients like you wanted but we can also retrieve all recipes that have certain ingredient(s) in them (this is the beauty of a many-to-many relationship) and on top of that it simplifies your queries.

Now for the original query you wanted with this new setup:

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

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

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

query will hold all of the ingredients for the recipe object when the query is executed.

Now suppose you want to create a query where you get all of the recipes that contain a certain ingredient:

ParseObject ingredient = ...

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

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

query will contain all Recipe objects that have the specified ingredient in their ingredients relation column when the query is executed.

I hope this helped you. Please let me know if I severely misunderstood the structure of your app - if so I will revise my answer if you give me new information but honestly I think the "middle man" RecipeIngredient is forcing you to complicate your app.

这篇关于ParseQueryAdapter钥匙匹配空的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆