Firebase-按顺序获取数据列表(最后一个数据优先) [英] Firebase - getting list of data in order (Last data first)

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

问题描述

我正在处理聊天应用程序.我想从Firebase数据库中获取最后20条消息,并按顺序添加了该消息.例如,如果我发送了3个不同的消息嗨",怎么了",再见".我应该以相同的顺序得到嗨",怎么了",再见".

I am working on a chat application. I want to get the last 20 messages from the Firebase database and in order, the message was added. For example, if I have sent 3 different message "Hi", "What's up", "Bye". I should get "Hi", "What's up", "Bye" in the same order.

我正在使用的查询如下.

The query I am using is following.

ref.child(Constants.messageListKey).child("-Laray524a9Na-C7zdij").queryLimited(toLast: Constants.messageLimitPerCall).observeSingleEvent(of: .value, with: { (snapshot) in 
       let currentUserChatList = snapshot.value as? NSDictionary
       if let chatList = currentUserChatList?.allKeys { 
         // parsing the data here
       }
}

我什至尝试使用 .queryOrderedByKey() .queryOrdered(byChild:),但结果仍然相同.

I even tried using .queryOrderedByKey() and .queryOrdered(byChild:) but still the same result.

我得到的结果不正确.例如,我收到最新消息",嗨",再见".结果甚至不是升序也不是降序.这只是一个随机顺序.

The result that I am getting is not in order. For example, I am getting "What's up", "Hi", "Bye". The result is not even ascending order nor descending order. It is just a random order.

我正在使用的方案是这样的:

The scheme which I am using is like this:

-chatList
    -autogeneratedKey
         -msg = "Some Message"

有什么我想念的吗?如果有任何我想念的细节,请让我知道,以更好地理解我的问题.

Is there anything that I am missing? If there is any details that I have missed please let me know to understand my question better.

推荐答案

我建议向您的节点添加时间戳,以保证顺序.然后,您还需要添加一个负的时间戳记,以启用降序排序

I would suggest adding a time stamp to you nodes which will guarantee the order. You would then also add a negative time stamp as well which would enable descending sort

messages
   msg_0
     msg: "What's up"
     timestamp: 20190927013000
     neg_timestamp: -20190927013000
   msg_1
     msg: "Hi"
     timestamp: 20190927020000
     neg_timestamp: -20190927020000

然后您可以查询,但是时间戳记为升序,而neg_timesstamp为降序,并且将被保证顺序.

Then you can query but timestampt for ascending and neg_timesstamp for descending and will be guarateeed order.

这些节点是随机顺序的,因为您将它们读为字典,它们是键,值对的无序集合.

The nodes are in random order because you're reading them as a Dictionary which are unordered sets of key: value pairs.

let currentUserChatList = snapshot.value as? NSDictionary

如果您希望它们按顺序排列,请执行此操作

If you want them IN order then do this

let currentUserChatList = snapshot.children.allObjects as! [DataSnapshot]

这将保持它们的顺序,您可以像问题中一样,使用for循环遍历它们.每个子节点也将是一个DataSnapshot,因此您可以使用以下方式访问子节点

which will maintain their order, and you can iterate over them with a for loop as in your question. Each child will be a DataSnapshot as well so you can access the child nodes with

let child in currentUserChatList {
   let msg = child.childSnapshot(forPath: "msg").value as? String ?? "No Msg"
}

这篇关于Firebase-按顺序获取数据列表(最后一个数据优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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