Firebase 按顺序获取数据 [英] Firebase getting data in order

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

问题描述

我正在使用 Firebase,直到最近我才按字母顺序获取数据.我从不使用查询,我总是使用数据的快照并一一排序.最近,snapVal 中的数据并不总是按字母顺序排列.我如何才能获得按字母顺序排序的 snapVal 数据,就像它在数据库的快照中一样?

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?

真实示例:有 4 条消息,id1-id4(按此顺序).它们包含消息1"-4".快照看起来正确.但是 snapVal (snapshot.value) 看起来像这样:

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

快照的样子:

["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";
}]

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

To get the snapVal, I use this:

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

澄清:

快照(最终结果正确):

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";
    };
}

这是 print(snapVal.keys) 的输出 if let snapVal = snapshot.value as?[字符串: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))

我的代码:

  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! 
Message 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")
                                }
                            }
                          }  
})

推荐答案

解决方案: 经过大量的搜索和尝试,问题仍然存在,一旦将快照转换为 snapVal (snapshot.value),顺序经常重新排列.我的(工作)解决方案:

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
 }
}

过程:

  1. 遍历快照中的每个孩子

  1. Loop through each child in snapshot

将子元素转换为 FIRDataSnapshot 使其不是元素

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

获取要访问的特定子项的值

Get the value of the particular child to access properties from

按照 NSDictionary 原则为孩子添加相应的代码.

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

为什么这个解决方案是可靠的

以正确的顺序接收快照非常简单.当我获得 snapshot.value 时,我面临的问题是以正确的顺序获取数据.该解决方案解决了这个问题,因为只有在循环遍历已排序的快照的子项时才访问每个子项的值.这使得孩子的顺序仍然在快照的控制之下.

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.

我也喜欢使用 [String: AnyObject]snapshot.value 方法,因为它非常接近 Swift 中 Firebase 实现的旧功能:简单且非常干净的.我其实认为这样使用 NSDictionary 从长远来看确实是一种节省时间的方式,因为它一点也不冗长.

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天全站免登陆