gpt4 book ai didi

swift - 当 .childMoved 仅假设被触发时,.childAdded 与 queryLimited 重复数据

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

编辑

addChatDataListener() 根据时间戳从用户聊天数据中获取 3 个结果,并且 addChatChangedListener() 监听以下数据:根据时间戳更改。假设有 5 个聊天 1,2,3,4,5,它根据时间戳获取 1,2,3,因此这些结果现在位于客户端或数组中。当我从 firebase 控制台手动将 chatID 3 时间戳更新为数据库中的最新时间戳时。只有 .childMoved 被解雇但是当我更改 chatID 4 (未被捕获)时。 .childMoved 和 .childAdded 都会因重复数据而被触发。

var tableView = UITableView()
var ref: DatabaseReference!
var chats = [Chat]()


override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.frame = self.view.frame
tableView.register(ChatTableViewCell.self, forCellReuseIdentifier: "chatCell")
self.view.addSubview(tableView)
ref = Database.database().reference()
addChatDataListener()
addChatChangeListener()
}

func addChatDataListener(){
(ref.child("user_chats").child(myUsername)).queryOrdered(byChild: "timestamp").queryLimited(toLast: 3).observe(.childAdded, with: {(snapshot) in
let chat = self.getChatObject(snapshot: snapshot)
print("child added: \(chat)")
self.chats.insert(chat, at: 0)
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
})
}
func addChatChangeListener(){
(ref.child("user_chats").child(myUsername)).queryOrdered(byChild: "timestamp").observe(.childMoved, with: {(snapshot) in
let index = self.indexOfChat(key: snapshot.key)
let chat = self.getChatObject(snapshot: snapshot)
print("child moved: \(index)")
print("child moved: \(chat)")
if(index == -1){
self.chats.insert(chat, at: 0)
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}else{
self.chats[index] = chat
let chat = self.chats.remove(at: index)
self.chats.insert(chat, at: 0)
self.tableView.moveRow(at: IndexPath(row: index, section: 0), to: IndexPath(row: 0, section: 0))
}
})
}

This is my database

最佳答案

问题是您正在使用数组 - 它们无法重新排序,并且在 NoSQL 数据库中使用非常具有挑战性,因为元素无法在数组范围内更改。通常(几乎总是)有更好的选择来构建和存储数据。

简而言之,代码中的数组可以删除索引,并且以下索引处的对象会“向上移动”并填充该位置。同样,可以将对象插入数组中,并且以下元素“下移”。

Firebase 中的数组不是这样工作的;要更改顺序、计数等,必须读取整个数组并进行更新,然后写回 - 因此会触发多个事件,如我的评论中所述。

查看这篇过时但切题的博客 Arrays Are Evil和一些进一步阅读here

你应该改变你的结构;

user_chats
uid_0
random_chat_id_0 //created with .childByAutoId
timestamp: 20191020
unread_messags: 3
user_id: uid_1 //don't use -> user_name: "some username"
random_chat_id_1
timestamp: 20191021
unread_messags: 3
user_id: uid_4

.childByAutoId 是您的 friend ,我通常建议使用它来创建必须唯一的任何类型的节点 key 。

另一个建议是避免使用用户名;使用用户 uid。用户名通常是动态的,但用户 ID 是静态的并与特定用户绑定(bind)。

最后一件事;函数 addChatChangeListener 不是这样的。有两种不同的查询观察者.childChanged.childMoved,它们的功能非常不同。我建议将该函数重命名为 addChatMovedLister 以避免混淆,或更改函数内的代码以实际处理 .childChanged 事件。

关于swift - 当 .childMoved 仅假设被触发时,.childAdded 与 queryLimited 重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460610/

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