【发布时间】:2019-09-28 17:49:40
【问题描述】:
我正在开发一个聊天应用程序。我想从 Firebase 数据库中获取最后 20 条消息,并按顺序添加了消息。 例如,如果我发送了 3 条不同的消息“Hi”、“What's up”、“Bye”。我应该按相同的顺序得到“Hi”、“What's up”、“Bye”。
我正在使用的查询如下。
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:),但结果仍然相同。
我得到的结果不正常。例如,我收到“What's up”、“Hi”、“Bye”。结果既不是升序也不是降序。这只是一个随机顺序。
我使用的方案是这样的:
-chatList
-autogeneratedKey
-msg = "Some Message"
我有什么遗漏的吗? 如果我遗漏了任何细节,请告诉我以更好地理解我的问题。
【问题讨论】:
-
Firebase 数据库查询始终按升序返回结果。如果您需要它们以降序排列,您要么必须在客户端反转它们,要么将反转的值存储在数据库中。见stackoverflow.com/q/45137899
标签: ios swift firebase firebase-realtime-database