【问题标题】:firebase objective c queryfirebase 目标 c 查询
【发布时间】:2017-05-11 11:06:43
【问题描述】:

我想使用以下函数检索当前用户 uid 设置为 yes 的对象,这似乎不起作用。

- (void)configureDatabase {
    _ref = [[FIRDatabase database] reference];

    FIRUser *user = [FIRAuth auth].currentUser;

    _refHandle = [[[[_ref child:@"tasks"] queryOrderedByChild:@"appliedByUsers"] queryEqualToValue:@YES childKey:user.uid] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot)

    {

    NSLog(@"snapshot: %@", snapshot);

    [_tasks addObject:snapshot];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_tasks.count-1 inSection:0]] withRowAnimation: UITableViewRowAnimationAutomatic];
}]; }
tasks
   -Kj3BawmhKjqbZXZdicq
     applicationStatus: 
     appliedByUsers
       bNDJtrn1Mmh51GXHTbMNR1MiTZt1: true
       hUI0W0TZxObazIFxSRm8t990UgM2: true
     price: 
     source: 
     taskName: 
     type: 
.
.
.

如何检索当前 user.uid 为 true 的任务?

【问题讨论】:

    标签: ios objective-c firebase firebase-authentication


    【解决方案1】:

    不要用childKey 传递user.uid,而是用queryOrderedByChildappliedByUsers 指定它。

    FIRUser *user = [FIRAuth auth].currentUser;
    
    _refHandle = [[[[_ref child:@"tasks"] queryOrderedByChild:[NSString stringWithFormat:@"appliedByUsers/%@",user.uid]] 
                      queryEqualToValue:@YES]                                       
                      observeEventType:FIRDataEventTypeChildAdded 
                      withBlock:^(FIRDataSnapshot *snapshot) {
    
         NSLog(@"snapshot: %@", snapshot);
    
         [_tasks addObject:snapshot];
         [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_tasks.count-1 inSection:0]] withRowAnimation: UITableViewRowAnimationAutomatic];
    }];
    

    注意:如果您的值true 不是布尔值,则它是queryEqualToValue:@"true"] 的字符串

    【讨论】:

    • 谢谢尼拉夫!我无法添加@"appliedByUsers/user.uid",因为它给了我以下错误:'InvalidPathValidation',原因:'(queryOrderedByChild:) 必须是非空字符串并且不包含'。' '#' '$' '[' or ']'' 还有其他方法可以将 user.uid 附加到路径中吗?
    • 太棒了!谢谢!:)
    【解决方案2】:

    Cloud Firestore 还允许您指定数据的排序顺序,并使用 orderBy() 和 limit() 指定要检索的文档数量限制。例如,您可以按字母顺序查询前 3 个城市:

    [[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
    

    您还可以按降序排序以获取最后 3 个城市:

    [[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
    

    您还可以按多个字段排序。例如,如果您想按州排序,并且在每个州内按人口降序排列:

    [[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" 
                descending:YES];
    

    您可以将 where() 过滤器与 orderBy() 和 limit() 结合使用。在以下示例中,查询定义了人口阈值,按人口升序排序,并仅返回超过阈值的前几个结果:

    [[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
                 queryOrderedByField:@"population"]
                 queryLimitedTo:2];
    

    但是,如果您有一个具有范围比较(、>=)的过滤器,则您的第一个排序必须在同一字段上:

    有效:范围过滤器和 orderBy 在同一字段上

    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
                queryOrderedByField:@"population"];
    

    无效:范围过滤器和不同字段上的 first orderBy

    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]  
                queryOrderedByField:@"country"];
    

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2018-04-29
      • 2015-12-05
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2018-04-12
      相关资源
      最近更新 更多