gpt4 book ai didi

ios - 如何将 Firebase 数据存储在数组中并打印出来?

转载 作者:可可西里 更新时间:2023-11-01 06:13:45 26 4
gpt4 key购买 nike

我一整天都在尝试将 firebase 数据存储到数组中并打印出来,但无济于事。我将非常感谢社区的帮助。

下图是当前 firebase 数据库的样子:

enter image description here

下面是从 firebase 读取数据的函数代码:

- (void)readDataFromServer {

_ref = [[FIRDatabase database] reference];

_hotelRef = [_ref child:@"hotel bookings"];

NSString *userID = [FIRAuth auth].currentUser.uid;

FIRDatabaseQuery *userHotelBookingsQuery = [[_hotelRef child:userID] queryOrderedByChild:@"number"];

[userHotelBookingsQuery observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

if (snapshot.value == [NSNull null]) {

NSLog(@"No messages");

} else {

[self.arrayOfBookingDetail removeAllObjects];

self.arrayOfBookingDetail = (snapshot.value);

NSString *firstMessage = [self.arrayOfBookingDetail objectAtIndex:0];

NSLog(@"First message is: %@", firstMessage);

}


}];

}

最佳答案

您的代码失败是因为您通过用户 uid 获得了实际上并不存在的 child 。 K2xxxxxxxx 是每个元素的唯一字符串,不对应于用户 ID。试试这个

_ref = [[FIRDatabase database] reference];

_hotelRef = [_ref child:@"hotel bookings"];

FIRDatabaseQuery *userHotelBookingsQuery = [_hotelRef queryOrderedByChild:@"number"];

[userHotelBookingsQuery observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

if (snapshot.value == [NSNull null]) {
NSLog(@"No messages");
} else {
//[self.arrayOfBookingDetail removeAllObjects];
//self.arrayOfBookingDetail = (snapshot.children);
//NSString *firstMessage = [self.arrayOfBookingDetail objectAtIndex:0];
//NSLog(@"First message is: %@", firstMessage);
NSDictionary *allUsersData = (NSDictionary*)snapshot.value;
for (NSString *key in allUsersData.allKeys) {
NSLog("@key is %@, data is %@",key,allUsersData[key]);
}
}

}];

}

编辑:

要查询特定用户 - 将您的查询更改为

 FIRDatabaseQuery *userHotelBookingsQuery = [[_hotelRef queryOrderedByChild:@"userID"] queryEqualToValue:@"GEmxxxxxxxxx"];

关于ios - 如何将 Firebase 数据存储在数组中并打印出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com