1

Working with recipes and I would like to query (search) based off of items in the recipe rather than the recipe names.

For example, multiple items may contain Chicken. I want to be able to search Chicken and see the Recipe Names that contain Chicken in the recipe.

Here's what I have tried:

- (void)filterResults:(NSString *)searchTerm
{
     PFQuery * query = [PFQuery queryWithClassName:self.parseClassName];
     NSArray * ingredientArray = [self.profileObject objectForKey:@"ingredients"];
     [query whereKey:searchTerm containedIn:ingredientArray];

     [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error)
        {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else
        {
            [self.searchResults removeAllObjects];
            [self.searchResults addObjectsFromArray:objects];
            [self.searchDisplayController.searchResultsTableView reloadData];
        }
    }];

}

This code returns nothing and I get no errors.

Having difficulty figuring out the right way to set up the query.

Should this be solved as a query within a query?

Meaning:

Query through ingredients first, and then query on that to display the recipe name based on the previous query of recipes that contain the searchTerm.

1
  • Please don't make us guess. What problem are you having? What happens with the code you posted? Commented Jan 19, 2014 at 23:22

2 Answers 2

2

I think you are misusing the [query whereKey:containedIn:] method. This is used to query for all PFObjects where the object for the key you specify is contained in the array you provide. Unless you made a new key for every recipe item, this won't work for your purposes, because none of your objects have a "Chicken" key, for instance.

First, I suggest you use a RecipeIngredient class in Parse with the following fields:

  • Pointer(Recipe) recipe //pointer to its Recipe object
  • Number amount //how much of unit
  • String unit //cup, gram, etc.
  • String ingredient //milk, flour, etc.

Now, you can simply query the RecipeIngredient class like so:

PFQuery * query = [PFQuery queryWithClassName:"RecipeIngredient"];
[query whereKey:"ingredient" equalTo:searchTerm];
[query includeKey:"recipe"]; //Fetches the Recipe data via the pointer
[query findObjectsInBackgroundWithBlock:^(NSArray *recipeIngredientObjects, NSError *error) {
     if (!error) {
         NSArray *recipes = [recipeIngredientObjects valueForKey:@"recipe"];
         //update table data as needed
     } else {
         // Log details of the failure
         NSLog(@"Error: %@ %@", error, [error userInfo]);
     }
}];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I dug a lot more into this last night and what I discovered is that the searchTerm has to match word-for-word what is in the array. So if I have "1 cup of flour", I have to type all of that in. Ideally I'd like to search just for flour and get the result.
Answer edited to show how to setup Parse to be able to search for specific ingredient names
@LukeIrvin Did you find solution to this? I am also having this issue.
1

I had this problem last year sometime so I thought I would share the answer.

    [query whereKey:@"ingredient" matchesRegex:searchTerm modifiers:@"i"];

That should do it for you. That helps with the case sensitivity.

Comments

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.