1

Im using Parse.com and I have 2 tables: recipe and ingredient, now in 1 recipe I have many ingredients In parse I've connected the table using parent in ingredient table, I'm trying to build an ArrayList of Recige (each Recipe contain an ArrayList of ingredients)

Something like this:

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

        query.orderByDescending("createdAt");
        query.include("Ingredient");

        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> RecipeList, ParseException e) {

//What should i write here ???
------------------------------

}

Thank a lot guys

ingrediant table: ObjectID ,NAME ,UNIT ,parent

4
  • 1
    Do you want to get all the ingredients belonging to a recipe? Or do you want to get all recipes that have an ingredient? Or do you want to get all recipes' ingredients? Commented Feb 17, 2015 at 10:24
  • I would like to see how your Ingredient table looks like. I assume you are using a pointer since you are using include. Is the pointed to object containing a list of ingredients, a single ingredient, or something else? Commented Feb 17, 2015 at 10:37
  • Hey I Would like to get a list of all recipies - each recipe has list of ingredients (so Iguess the answer is both) Commented Feb 17, 2015 at 12:43
  • should I use arrays? How do i do that :) 10x Commented Feb 17, 2015 at 12:53

1 Answer 1

1

Since ingredients only belong to a recipe list, I assume that recipies have a reference to the ingredients that belong to their list.

If your reference are pointers to all the ingredients of a recipe then this should suffice

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipies");
    query.include("ingredients"); //Name of your ingredients table
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for(int index = 0; index < objects.size(); i++){
                    objects.get(i); //This is a recipe
                    objects.get(i).get("name of the column that hold the pointer"); //This is your ingredient
                }
            } else {
                Log.e("", e.getMessage());
            }
            functionCallback.done(objects, e);
        }

    });

More information about 'include' query can be found here (under Relational Queries)

If you store the reference in an array you could try and do something like this

    ParseQuery<ParseObject> ingredientQuery = ParseQuery.getQuery("ingredients");

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipies");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for(int index = 0; index < objects.size(); i++){
                    objects.get(i); //This is a recipe
                    String[] pointers = objects.get(i).get("name of the column that hold the pointer"); 
                    //This is your ingredient reference (if you did 'recipe.put("ingredients", ingredient);', 
                    //the reference that would be saved is the id of the ingredient.
                    ingredientsQuery.whereContainedIn("objectId", pointers);
                    ingredientsQuery.findInBackground(new FindCallback<ParseObject>() {

                    @Override
                    public void done(List<ParseObject> ingredients, ParseException e) {
                    if (e == null) {
                        for(int index = 0; index < ingredients.size(); i++){
                            ingredients.get(i); //This is a ingredient
                        }
                    } else { 
                        Log.e("", e.getMessage());
                    }
               }
            } else { 
                Log.e("", e.getMessage());
            }
            functionCallback.done(objects, e);
        }

    });

Think this should about do it!

Sign up to request clarification or add additional context in comments.

4 Comments

currently I have a pointer to the Recipe in the ingredient.... as for as i know i suppose to put FK in the many side? so no, i do not have ingredient pointer in recipe... maybe this is why im having a problem? thank you so much :)
Sorry for my late response. I think since you do not have a pointer to an Ingredient object inside your recipe, it isn't able to include ingredients. This is not a problem tho. You just have to retrieve all ingredients that have a pointer to the recipe you want.
Ya I did what you said and it worked.... but im wondering ... is it efficient? for each recipe Im looping the whole Ingredient table...
I wouldn't know if it's efficient or not. All I know is that when you use a pointer to an object, it doesn't compare the entire object, but just the unique objectId. I've been using multi-levelled references (aka person <-> personParty <-> Party) and I havn't had any performance issues. Either calling all persons at a party, or calling all the parties a person goes to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.