gpt4 book ai didi

swift - 在 Swift 3 中刷新 Collection View 后点击发送按钮时会出现重复数据吗?

转载 作者:行者123 更新时间:2023-11-30 11:31:43 26 4
gpt4 key购买 nike

我在使用 JSQMessage 库和 Firebase 的聊天应用程序中遇到了此错误。当我刷新后按 Enter 键以在 Collection View 中加载更多消息时,消息会重复吗?

加载更多功能可以工作,但是当点击输入新消息时会重复。

我尝试删除数组中的所有元素并重新加载 Collection View ,但仍然不起作用。点击发送按钮时也会重新加载 Collection View ,但出现相同的错误。

我发现当我拉刷新 Collection View 时出现错误。刷新 1 次,重复 1 次,刷新 2 次,重复 2 次...

任何人请帮我解决这个问题。感谢您的帮助。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
// set up refresh ctrl
refreshControl.tintColor = #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1)
refreshControl.addTarget(self, action: #selector(loadMore), for: .valueChanged)
collectionView.addSubview(refreshControl)
}

func loadMore() {
Array_Messages.removeAll()
ClassMessages.removeAll()
DispatchQueue.main.async {
self.collectionView.reloadData()
}

loadMess += 10 // number of mess when load more
// numMess is the total of message, loadMess is the number of showing message
if loadMess > numMess {
loadMess = numMess
getData(numberMess: numMess)
collectionView.scrollsToTop = true
refreshControl.endRefreshing()

} else {
getData(numberMess: loadMess)
collectionView.scrollsToTop = true
refreshControl.endRefreshing()
}

}

// get data
func getData(numberMess : Int) {
guard let fromId = self.senderId else { return }
guard let toId = receiverId else { return }

databaseReference.child("Conversation").child(fromId).child(toId).queryLimited(toLast:UInt(numberMess)).observe(.childAdded) { (snapshot) in
self.databaseReference.child("Message").child(snapshot.key).observeSingleEvent(of: .value, with: { (snapshot) in

if let dict = snapshot.value as? [String : Any] {
let receiverid = dict["ReceiverId"] as? String ?? ""
let senderid = dict["SenderId"] as? String ?? ""
let time = dict["Time"] as? NSNumber ?? 0
let mediatype = dict["MediaType"] as? String ?? ""
let status = dict["Status"] as? String ?? ""
let sendername = dict["SenderName"] as? String ?? ""
let receivername = dict["ReceiverName"] as? String ?? ""

if mediatype == "Text" {
let text = dict["Text"] as! String
let newJSQMess = JSQMessage(senderId: senderid, displayName: sendername, text: text)

let newMess = Messages(TextString: text, SenderIdString: senderid, SenderNameString: sendername, ReceiverNameString: receivername, ReceiverIdString: receiverid, TimeNumber: time, StatusString: status)

if newMess.SenderId == self.senderId {
newMess.ChatId = newMess.ReceiverId
} else {
newMess.ChatId = newMess.SenderId
}

if newMess.ChatId == self.receiverId {
self.Array_Messages.append(newJSQMess!)
self.ClassMessages.append(newMess)
self.finishReceivingMessage()
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}


}
})
}
}

这是我的错误的图像。因为我没有足够的声誉所以请点击链接查看图片/image/J1ir3.jpg

最佳答案

我找到了自己问题的解决方案。只需将删除所有观察添加到 loadMore() 函数的顶部即可。

databaseReference.child("Conversation").child(fromId).child(toId).removeAllObservers()

关于swift - 在 Swift 3 中刷新 Collection View 后点击发送按钮时会出现重复数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50187436/

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