本文介绍了IOS Firebase:将snapshot.value中的所有关键点都弄乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从下面的日志中收到来自Firebase服务器的快照。现在我想快照循环所有的键,但我得到了凌乱的位置。

我不知道为什么会发生这种情况。任何帮助将不胜感激



这里是打印日志的代码

  NSLog(@snapshot =%@,snapshot.value); 
for(NSString * keyPath in snapshot.value){
NSLog(@keyPath =%@,keyPath);

$ / code>

这里是日志

  2016-04-20 11:48:04.177 EarCrush [5241:79090] snapshot = {
-KFlrXzqsSa50GVnJkqk= {
conversationID =2886- 2377\" ;
text = Okteh;
time =20-04-2016 11:12;
};
-KFlrgrvdR_uj2o1ltaw= {
conversationID =2886-2377;
text =你怎么确定;
time =20-04-2016 11:12;
};
-KFlrhSBbVnx4XyWjQPy= {
conversationID =2886-2377;
text =Message 2 .....;
time =20-04-2016 11:12;
};
-KFlrhzx0TK5CeSR3aAA= {
conversationID =2886-2377;
text =Message 3:Hello world;
time =20-04-2016 11:12;
};

2016-04-20 11:48:04.177 EarCrush [5241:79090] keyPath = -KFlrhzx0TK5CeSR3aAA
2016-04-20 11:48:04.178 EarCrush [5241:79090] keyPath = -KFlrhSBbVnx4XyWjQPy
2016-04-20 11:48:04.179 EarCrush [5241:79090] keyPath = -KFlrXzqsSa50GVnJkqk
2016-04-20 11:48:04.179 EarCrush [5241:79090] keyPath = - FireFox服务器映像

解决方案

打印FDataSnapshot时,会打印JSON结构。由于JSON没有为子节点定义的顺序,所以它们的显示顺序是不确定的(尽管经常会按照它们的键字顺序打印孩子)。

可以用于以特定顺序获取数据。例如从:

 <$ c (FDataSnapshot * snapshot.children中的子元素){
$ b()){
$ b(
$ b $) $ b NSLog(@child.key =%@,child.key);


Firebase快照包含有关子订单的其他信息数据,当您打印该快照时它不显示。当您循环快照的子项时,此订购信息用于确保上面的循环中正确的顺序。


I receive the snapshot from Firebase server like the log below. Now I want to loop all key in snapshot but I got messy position.

I don't know why this happened. Any help would be great appreciated

Here is the code to print log

NSLog(@"snapshot = %@",snapshot.value);
for (NSString *keyPath in snapshot.value) {
     NSLog(@"keyPath = %@",keyPath);
}

Here is the log

2016-04-20 11:48:04.177 EarCrush[5241:79090] snapshot = {
    "-KFlrXzqsSa50GVnJkqk" =     {
        conversationID = "2886-2377";
        text = Okteh;
        time = "20-04-2016 11:12";
    };
    "-KFlrgrvdR_uj2o1ltaw" =     {
        conversationID = "2886-2377";
        text = "How are you determining";
        time = "20-04-2016 11:12";
    };
    "-KFlrhSBbVnx4XyWjQPy" =     {
        conversationID = "2886-2377";
        text = "Message 2 ..... ";
        time = "20-04-2016 11:12";
    };
    "-KFlrhzx0TK5CeSR3aAA" =     {
        conversationID = "2886-2377";
        text = "Message 3: Hello world ";
        time = "20-04-2016 11:12";
    };
}
2016-04-20 11:48:04.177 EarCrush[5241:79090] keyPath = -KFlrhzx0TK5CeSR3aAA
2016-04-20 11:48:04.178 EarCrush[5241:79090] keyPath = -KFlrhSBbVnx4XyWjQPy
2016-04-20 11:48:04.179 EarCrush[5241:79090] keyPath = -KFlrXzqsSa50GVnJkqk
2016-04-20 11:48:04.179 EarCrush[5241:79090] keyPath = -KFlrgrvdR_uj2o1ltaw

Firebase server image

解决方案

When you print a FDataSnapshot, it prints the JSON structure. Since JSON has no defined order for child nodes, the order in which they are shown is undetermined (although frequently it will print the children in lexicographical order of they key as you get here).

Firebase's queries can be used to get the data in a specific order. For example from this question:

[[languagesRef queryOrderedByChild:@"distance"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    for ( FDataSnapshot *child in snapshot.children) {

        NSLog(@"child.key = %@",child.key);

    }

The Firebase snapshot contains additional information about the ordering of the child data, that it doesn't show when you print that snapshot. When you loop over the children of the snapshot, this ordering information is used to ensure the correct order in the loop above.

这篇关于IOS Firebase:将snapshot.value中的所有关键点都弄乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:02