【问题标题】:Parse.com Query if String is Contained in ArrayParse.com 查询字符串是否包含在数组中
【发布时间】:2014-02-09 00:46:48
【问题描述】:

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

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

这是我尝试过的:

- (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的食谱进行查询以显示食谱名称。

【问题讨论】:

  • 请不要让我们猜测。你有什么问题?您发布的代码会发生什么情况?

标签: ios objective-c search filter parse-platform


【解决方案1】:

我认为您在滥用[query whereKey:containedIn:] 方法。这用于查询您指定的键的对象包含在您提供的数组中的所有 PFObjects。除非您为每个配方项目创建一个新密钥,否则这对您的目的不起作用,因为您的对象都没有“鸡”密钥,例如。

首先,我建议您在 Parse 中使用 RecipeIngredient 类,其中包含以下字段:

  • Pointer(Recipe) 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]);
     }
}];

【讨论】:

  • 谢谢。昨晚我对此进行了深入研究,我发现 searchTerm 必须逐字匹配数组中的内容。因此,如果我有“1 杯面粉”,我必须输入所有内容。理想情况下,我只想搜索面粉并获得结果。
  • 答案已编辑以显示如何设置 Parse 以便能够搜索特定成分名称
  • @LukeIrvin 你找到解决方案了吗?我也有这个问题。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2013-08-16
    • 2011-05-14
    • 1970-01-01
    • 2016-01-31
    • 2022-06-30
    • 2021-12-14
    • 2021-06-10
    • 1970-01-01
    • 2017-01-18
    相关资源
    最近更新 更多