简体   繁体   中英

Parse.com Query if String is Contained in Array

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM