Firebase 目标 C 查询



我想使用以下函数检索当前用户 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 为真的任务?

与其传递user.uidchildKey不如用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"]

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

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

有效:同一字段上的范围筛选器和排序依据

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

无效:不同字段上的范围筛选器和一阶

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

最新更新