Firebase正在获取数据 [英] Firebase getting data in order

查看:114
本文介绍了Firebase正在获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase,直到最近才按字母顺序获取数据。我从来没有使用查询,我总是只使用数据的快照,并通过他们一一排序。最近, snapVal 中的数据并不总是按字母顺序排列。我如何做到这一点,所以我得到一个数据snapVal按字母顺序排序,就像它在数据库的快照?

实例:有4个消息,id1- id4(按此顺序)。他们坚持信息1 - 4。快照看起来正确。但是,snapVal(snapshot.value)看起来像这样:

pre code $ [id2:{
DATE =10 / 20 / 16\" ;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 2;
TIME =8:12 PM;
},id4:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 4;
TIME =8:12 PM;
},id1:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 1;
TIME =8:12 PM;
},id3:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 3;
TIME =8:12 PM;
}]

快照看起来像:

  [id1:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 1;
TIME =8:12 PM;
},id2:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 2;
TIME =8:12 PM;
},id3:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 3;
TIME =8:12 PM;
},id4:{
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 4;
TIME =8:12 PM;
}]

为了得到snapVal,我使用这个:

 如果让snapVal = snapshot.value as? [String:AnyObject] {
//不按顺序排列..
}



<为了澄清:
$ b $快照(这个结果出来正确):

  Snap(CHAT){
id1 = {
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 1;
TIME =8:12 PM;
};
id2 = {
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 2;
TIME =8:12 PM;
};
id3 = {
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 3;
TIME =8:12 PM;
};
id4 = {
DATE =10/20/16;
FIRST_NAME=第一个;
ID = userID;
LAST_NAME= last;
MESSAGE = 4;
TIME =8:12 PM;
};



$ b

这是 print(snapVal.keys )里面如果让snapVal = snapshot.value as? [String:AnyObject]

  LazyMapCollection< Dictionary< String,AnyObject> ;, String> :[id2:{
DATE =10/20/16;
FIRST_NAME= first;
ID = userID;
LAST_NAME= last;
MESSAGE = 2;
TIME =8:12 PM;
},id4:{
DATE =10/20/16;
FIRST_NAME= first;
ID = userID;
LAST_NAME= last;
MESSAGE = 4;
TIME =8:12 PM;
}, id1:{
DATE =10/20/16;
FIRST_NAME= first;
ID = userID;
LAST_NAME= last;
MESSAGE = 1;
TIME =8:12 PM;
},id3:{
DATE =10/20/16;
FIRST_NAME = first;
ID = userID;
LAST_NAME= last;
MESSAGE = 3;
TIME =8:12 PM;
}],_transform :($)
$ / code $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ pre> self.firebase.child(Chats)。child(chatID).queryOrderedByKey()。obse rveSingleEvent(:.value,其中:{(snapshot)in
print(snapshot)
如果让snapVal = snapshot.value as? [String:AnyObject] {

print(snapVal)

for snapVal {
print(将消息检查为子对象)
让消息= c.value [MESSAGE] as?字符串

让fn = c.value [FIRST_NAME]为?字符串
让ln = c.value [LAST_NAME]为?字符串

让USER_ID = c.value [ID]为? String
$ b $如果让userID = USER_ID {
如果让msg = message {
如果let firstName = fn {
if let lastName = ln {
let username =\(firstName)\(lastName)
self.addMessage(userID,text:msg,name:username)

print(Message added!\\\
nssage Info :)
print(User ID:\(userID))
print(text:\(msg))
print(用户名:\(用户名) )
} else {
print(LN does not pass)
}
print(FN did not pass)
}
} else {
print(Msg did not pass)
}
} else {
print(User ID did not pass)
}
}
}
})
解决方案:经过非常广泛的搜索和尝试后,问题仍然坚持一旦快照转换为snapVal(snapshot.value),通常重新排列顺序。我的(工作)解决方案:

 为snapshot.children中的孩子{
let child = child as! FIRDataSnapshot
如果让childVal = child.value为? [String:AnyObject] {
let childMessage = childVal [MESSAGE] as!字符串
//每个孩子更多的代码。这个孩子可能是一种类似的帖子,你可以像我在
}
}




  • 循环通过快照中的每个孩子


  • 将孩子转换为FIRDataSnapshot,所以它不是一个元素


  • 为孩子添加相应的NSDictionary原则代码。

  • li>

    为什么这个解决方案是稳固的

    接收快照在正确的顺序是非常简单的。当我得到 snapshot.value 时,我遇到的问题是以正确的顺序获取数据。这个解决方案解决了这个问题,因为每个孩子的值只有在循环完成快照的孩子时才被访问。这留下了儿童的顺序仍然在快照的控制。

    我还喜欢 snapshot.value 方法,使用 [String:AnyObject] ,因为它非常接近Swift中Firebase实现的旧功能:简单且非常干净。我实际上认为以这种方式使用NSDictionary实际上是一种节省时间的方法,因为它不是以任何方式冗长的。


    I am using Firebase and I have had no problems getting data in alphabetical order until recently. I never used queries, I always just used snapshots of data and sorted through them one-by-one. Recently, the data has not been always coming in alphabetical order in the snapVal. How do I make it so I get a snapVal of data sorted alphabetically, like it is in the snapshot from the database?

    Real Example: there are 4 messages, id1-id4 (in that order). They contiain the message "1"-"4". The snapshot comes looking correct. But the snapVal (snapshot.value) looks like this:

    ["id2": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 2;
        TIME = "8:12 PM";
    }, "id4": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 4;
        TIME = "8:12 PM";
    }, "id1": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 1;
        TIME = "8:12 PM";
    }, "id3": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 3;
        TIME = "8:12 PM";
    }]
    

    What the snapshot looks like:

    ["id1": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 1;
        TIME = "8:12 PM";
    }, "id2": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 2;
        TIME = "8:12 PM";
    }, "id3": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 3;
        TIME = "8:12 PM";
    }, "id4": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 4;
        TIME = "8:12 PM";
    }]
    

    To get the snapVal, I use this:

    if let snapVal = snapshot.value as? [String: AnyObject] {
         // Comes out of order..
    }
    

    To clarify:

    Snapshot (this ends up coming out correct):

    Snap (CHAT) {
        id1 =     {
            DATE = "10/20/16";
            "FIRST_NAME" = first;
            ID = userID;
            "LAST_NAME" = last;
            MESSAGE = 1;
            TIME = "8:12 PM";
        };
        id2 =     {
            DATE = "10/20/16";
            "FIRST_NAME" = first;
            ID = userID;
            "LAST_NAME" = last;
            MESSAGE = 2;
            TIME = "8:12 PM";
        };
        id3 =     {
            DATE = "10/20/16";
            "FIRST_NAME" = first;
            ID = userID;
            "LAST_NAME" = last;
            MESSAGE = 3;
            TIME = "8:12 PM";
        };
        id4 =     {
            DATE = "10/20/16";
            "FIRST_NAME" = first;
            ID = userID;
            "LAST_NAME" = last;
            MESSAGE = 4;
            TIME = "8:12 PM";
        };
    }
    

    This is the output for print(snapVal.keys) inside if let snapVal = snapshot.value as? [String: AnyObject]:

    LazyMapCollection<Dictionary<String, AnyObject>, String>(_base: ["id2": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 2;
        TIME = "8:12 PM";
    }, "id4": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 4;
        TIME = "8:12 PM";
    }, "id1": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 1;
        TIME = "8:12 PM";
    }, "id3": {
        DATE = "10/20/16";
        "FIRST_NAME" = first;
        ID = userID;
        "LAST_NAME" = last;
        MESSAGE = 3;
        TIME = "8:12 PM";
    }], _transform: (Function))
    

    My Code:

      self.firebase.child("Chats").child(chatID).queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
                              print(snapshot)
                            if let snapVal = snapshot.value as? [String: AnyObject] {
    
                                print(snapVal)
    
                                for c in snapVal {
                                    print("checking Message as child")
                                    let message = c.value["MESSAGE"] as? String
    
                                    let fn = c.value["FIRST_NAME"] as? String
                                    let ln = c.value["LAST_NAME"] as? String
    
                                    let USER_ID = c.value["ID"] as? String
    
                                    if let userID = USER_ID {
                                        if let msg = message {
                                            if let firstName = fn {
                                                if let lastName = ln {
                                                    let username = "\(firstName) \(lastName)"
                                                    self.addMessage(userID, text: msg, name: username)
    
                                                    print("Message added! \nMessage Info:")
                                                    print("User ID: \(userID)")
                                                    print("text: \(msg)")
                                                    print("Username: \(username)")
                                                } else {
                                                    print("LN did not pass")
                                                }
                                            } else {
                                                print("FN did not pass")
                                            }
                                        } else {
                                            print("Msg did not pass")
                                        }
                                    } else {
                                        print("User ID did not pass")
                                    }
                                }
                              }  
    })
    

    解决方案

    Solution: After very extensive searching and attempting, the problem still persisted that once the snapshot was converted to a snapVal (snapshot.value), the order often rearranged. My (working) solution:

    for child in snapshot.children {
    let child = child as! FIRDataSnapshot
     if let childVal = child.value as? [String: AnyObject] {
        let childMessage = childVal["MESSAGE"] as! String
        // more code for each child. This child might be a post of a sort, which you can access properties of in a way like I did in the line above
     }
    }
    

    Process:

    1. Loop through each child in snapshot

    2. Convert the child to a FIRDataSnapshot so it is not an element

    3. Get the value of the particular child to access properties from

    4. Add in the respective code for the child following NSDictionary principles.

    Why this solution is solid

    Receiving snapshots in the proper order is very simple. The issue I faced was getting data in the correct order when I got the snapshot.value. This solution fixes that because the values of each child are only accessed when looping through the children of snapshot, which is sorted. This leaves the order of children still in the control of the snapshot.

    I also like the snapshot.value approach by using [String: AnyObject] because it is very close to the old functionality of Firebase implementation in Swift: Simple and very clean. I actually think that using NSDictionary in this way is really a way to save time in the long run because it is not verbose in any way.

    这篇关于Firebase正在获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆