gpt4 book ai didi

ios - Firebase 每次查询返回不同的值

转载 作者:行者123 更新时间:2023-11-28 20:59:32 26 4
gpt4 key购买 nike

我正在使用 Firebase 检查用户是否正在输入。

但每次我从 Firebase 查询值。值(value)观的种类不同。我的意思是,有时这些值是一个数组、一个字典、一个具有空值的数组……所以当我尝试解析该值时,我的应用程序崩溃了。

这是我的代码:

- (void)listenUserTypingGate {

__weak typeof(self)this = self;

[[_userTypingRef queryOrderedByKey] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if (this && snapshot.exists) {
NSDictionary* typingData = snapshot.value;
NSLog(@"ref: %@", _userTypingRef);
NSLog(@"ref query: %@", [_userTypingRef queryOrderedByKey]);
NSLog(@"data: %@", typingData);
}
}];
}

结果:

  • 我去对话1。它是一个数组

ref: https://testfir-199902.firebaseio.com/test-test/user_typing/0_1

ref query: (/test-test/user_typing/0_1 { i = ".key"; })

data: ( { avatar = ""; "user_typing" = 0; username = Oanh; }, { avatar = ""; "user_typing" = 0; username = "B\U00e9o"; } )

  • 对话2:是数组+空值

ref: https://testfir-199902.firebaseio.com/test-test/user_typing/2_3

ref query: (/test-test/user_typing/2_3 { i = ".key"; })

data: ( "", "", { avatar = ""; "user_typing" = 0; username = "H\U1ea1nh"; }, { avatar = ""; "user_typing" = 0; username = "L\U00f9n"; } )

  • 对话 3:它是一本字典

ref: https://testfir-199902.firebaseio.com/test-test/user_typing/4_5

ref query: (/test-test/user_typing/4_5 { i = ".key"; })

data: { 4 = { avatar = ""; "user_typing" = 0; username = "Ti\U1ebfn"; }; 5 = { avatar = ""; "user_typing" = 0; username = "L\U00ea"; }; }

这是我在 Firebase 上的结构: enter image description here

最佳答案

不要拍摄 snapshot.value。因为你的最后一个节点有一个 child 而不是一个值。

所以你必须首先检索每个 child 作为快照,然后从特定 child 检索一个值..

这里最后一个 Root 节点实际上是 0 和 1 而不是 0_1。

    NSArray*childs = snap.children;
for(int i = 0, i < childs.count , i++ )
{
FIRDataSnapshot*snap = childs[i]
NSDictionary* typingData = snap.value;
NSLog(@"ref: %@", _userTypingRef);
NSLog(@"ref query: %@", [_userTypingRef queryOrderedByKey]);
NSLog(@"data: %@", typingData);

}

关于ios - Firebase 每次查询返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152813/

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