如何订购Firebase数据?

时间:2018-03-12 18:40:49

标签: ios swift firebase firebase-realtime-database

我正在创建论坛/新闻Feed类型的应用程序,目前正在尝试创建实际的新闻Feed /论坛。到目前为止,我有一个tableview,其中包含由用户的显示名称,个人资料图片和帖子文本组成的帖子。

我遇到的问题是它没有订购。它以不可预测的顺序显示,每次重新启动应用程序时都会更改。我尝试使用" .queryOrdered(byChild:" Timestamp")"但它似乎没有起作用。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

当我将代码简化为:

var databaseRef = Database.database().reference()
databaseRef.child("49242360/Posts").queryOrdered(byChild: "Timestamp").observe(.value, with: { (snapshot) in        
    for child in (snapshot.children) {
        let snap = child as! DataSnapshot
        let dict = snap.value as! [String:AnyObject]
        let timestamp = dict["Timestamp"]!

        print("Key \(snap.key), Timestamp \(timestamp)")
    }
})
{(error) in
    print("Error: \(error.localizedDescription)")
}

打印:

Key -L7I6Bc5MeUgiOxkzKhF, Timestamp 1520739076.673202
Key -L7I6dBS7f43e49Xuhk1, Timestamp 1520739193.693312
Key -L7IRX8Uj89LLvymgTjA, Timestamp 1520744669.853121
Key -L7IReX4TqzXcwlbphji, Timestamp 1520744704.257354
...

第二轮:

Key -L7I6Bc5MeUgiOxkzKhF, Timestamp 1520739076.673202
Key -L7I6dBS7f43e49Xuhk1, Timestamp 1520739193.693312
Key -L7IRX8Uj89LLvymgTjA, Timestamp 1520744669.853121
Key -L7IReX4TqzXcwlbphji, Timestamp 1520744704.257354
...

因此在订购时看起来非常可靠。

当我将其展开以记录UID然后加载用户名时,我得到以下代码:

var databaseRef = Database.database().reference()
databaseRef.child("49242360/Posts").queryOrdered(byChild: "Timestamp").observe(.value, with: { (snapshot) in

    for child in (snapshot.children) {
        let snap = child as! DataSnapshot
        let dict = snap.value as! [String:AnyObject]
        let timestamp = dict["Timestamp"]!

        let userWhoPosted = dict["User"] as! String

        print("Key \(snap.key), Timestamp \(timestamp), User \(userWhoPosted)")

        databaseRef.child("49242360/Users").child(userWhoPosted).observeSingleEvent(of: .value, with: {
            (snapshot) in
            if let userDict = snapshot.value as? [String: AnyObject]
            {
                print("User \(snapshot.key), Name \(userDict["Display Name"]!)")
            }
        })
    }
})
{(error) in
    print("Error: \(error.localizedDescription)")
}

这个输出:

Key -L7I6Bc5MeUgiOxkzKhF, Timestamp 1520739076.673202, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7I6dBS7f43e49Xuhk1, Timestamp 1520739193.693312, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7IRX8Uj89LLvymgTjA, Timestamp 1520744669.853121, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7IReX4TqzXcwlbphji, Timestamp 1520744704.257354, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7IS-vOHvHaSWp_HRiT, Timestamp 1520744795.927767, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7IcTRvi0dL9HCPTiSE, Timestamp 1520747800.437907, User HfxlCKx4uYhf09TF8fPITdNQtlo1
Key -L7IdWlpJ8zLwI3YIvi7, Timestamp 1520748076.208767, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7IdboVraORS1-xTQMM, Timestamp 1520748100.956265, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7IgTX0XFbu9YC2mX6W, Timestamp 1520748849.399632, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7IvDLm1M9lmMJDmu5u, Timestamp 1520752715.24575, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7JOOepHWvxU6CbaEPO, Timestamp 1520760625.902419, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7JOS1zOsB9Tf0TCsOL, Timestamp 1520760639.73502, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7JWCQmnZhqmxC-vYqC, Timestamp 1520762672.943332, User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2
Key -L7JhX9BImuv0DAtjeBN, Timestamp 1520765903.561222, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7JhYbvMr-xvUCcZUdo, Timestamp 1520765909.560808, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7KtGXoMUEgAorrfCDm, Timestamp 1520785758.446064, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7KwK-VtU9vKtLg8F-x, Timestamp 1520786559.068727, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7KwQRUpjEa9fDTLz3g, Timestamp 1520786585.436313, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7Kwgo_XBLCjomFipQ5, Timestamp 1520786656.609396, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7L-X5vy3THn_bfq7vM, Timestamp 1520787661.309544, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7LcY7xebZPkIkmZ3dX, Timestamp 1520798151.346764, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7NOP4oE8ezfD8XyXxs, Timestamp 1520827736.498612, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7NOSlCNPTxTautJNOl, Timestamp 1520827751.563096, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7NVUR_P1yu5GFgism5, Timestamp 1520829593.437312, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7NZoYHvCeqXXUjOUZC, Timestamp 1520830728.45705, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7NZv6K8EtI97ZRHOl7, Timestamp 1520830755.339643, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7N_MvoCFV8nOszdcQn, Timestamp 1520830873.384209, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7QBKRzDxu-SvGGDs8S, Timestamp 1520874641.275255, User PQlpt0gStuNH5qICZFzTJYWrhjx1
Key -L7QCQueKovJpOVhxEfR, Timestamp 1520874929.892702, User PQlpt0gStuNH5qICZFzTJYWrhjx1
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User HfxlCKx4uYhf09TF8fPITdNQtlo1, Name dkkdd
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User 2ul1ZpfLtxTv4KodiNBLUvKAcgo2, Name joeyhhh
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd
User PQlpt0gStuNH5qICZFzTJYWrhjx1, Name dnndddmdmd

这对我来说再好看,虽然我绝对建议只加载每位用户一次(并保留最近已加载的用户地图)。

答案 1 :(得分:0)

此:

databaseRef.child("Posts").queryOrdered(byChild: "Timestamp").observe(.value, with: { (snapshot) in

是正确的,但在数据库中,您有许多具有相同值的TimeStamp "Timestamp" : 1.520786656609396E9,,这就是每次获得不同订单时的原因。