firebase objective c查询

问题描述 投票:0回答:2

我想使用以下函数检索当前用户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
2个回答
1
投票

而不是通过user.uid传递childKey,用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不是boolean而字符串是queryEqualToValue:@"true"]


1
投票

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"];

无效:范围过滤器和第一个订单在不同的字段上

[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]  
            queryOrderedByField:@"country"];
© www.soinside.com 2019 - 2024. All rights reserved.