Parse.com 查询字符串是否包含在数组中



使用食谱,我想根据食谱中的项目而不是食谱名称进行查询(搜索)。

例如,多个项目可能包含鸡肉。我希望能够搜索鸡肉并查看食谱中包含鸡肉的食谱名称。

这是我尝试过的:

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

这段代码不返回任何内容,也没有收到任何错误。

难以找出设置查询的正确方法。

这是否应该作为查询中的查询来解决?

意义:

首先查询成分,然后查询该成分,以根据包含 searchTerm 的食谱的上一个查询显示配方名称。

我认为您滥用了[query whereKey:containedIn:]方法。这用于查询您指定的键的对象包含在您提供的数组中的所有 PFObject。除非您为每个食谱项创建一个新键,否则这将不适用于您的目的,因为例如,您的对象都没有"Chicken"键。

首先,我建议您在 Parse 中使用具有以下字段的 RecipeIngredient 类:

  • 指针(配方) recipe//指向其Recipe对象的指针
  • 数量//单位多少
  • 字符串单位//杯、克等
  • 字符串成分//牛奶、面粉等

现在,您可以简单地查询RecipeIngredient类,如下所示:

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

去年的某个时候我遇到了这个问题,所以我想我会分享答案。

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

这应该为你做。这有助于区分大小写。

最新更新