gpt4 book ai didi

ios - 按时间顺序排列聊天。

转载 作者:行者123 更新时间:2023-11-30 11:46:39 25 4
gpt4 key购买 nike

我想按时间顺序显示聊天记录。我正在使用 Firebase 和 JSQMessageViewController。我认为问题出在observeConversations 函数中的某个地方。但是我还没有找到按时间顺序显示聊天的正确方法。它们目前完全随机显示。

 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
observeConversations()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return conversations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! ChatTableViewCell
cell.setConversation(conversations[indexPath.row])
return cell
}

func observeConversations() {
guard let user = Auth.auth().currentUser else { return }
let ref = Database.database().reference().child("conversations/users/\(user.uid)")
ref.observe(.value, with: { snapshot in

var _conversations = [Conversation]()
for child in snapshot.children {

if let childSnap = child as? DataSnapshot,
let dict = childSnap.value as? [String:Any],
let key = dict["key"] as? String,
let sender = dict["sender"] as? String,
let recipient = dict["recipient"] as? String,
let text = dict["text"] as? String,
let timestamp = dict["timestamp"] as? Double,
let muted = dict["muted"] as? Bool, !muted,
let seen = dict["seen"] as? Bool {

let date = Date(timeIntervalSince1970: timestamp/1000)
let conversation = Conversation(key: key, sender: sender, recipient: recipient, date: date, recentMessage: text, seen: seen)
_conversations.append(conversation)
}
}
self.conversations = _conversations
self.tableView.reloadData()

})
}

最佳答案

如果您想检索按时间戳排序的项目:

let ref = Database.database().reference().child("conversations/users/\(user.uid)")
ref.orderQuery(byChild: "timestamp").observe(.value, with: { snapshot in

关于ios - 按时间顺序排列聊天。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48754059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com