在 Swift 3 中刷新集合视图后点击发送按钮时出现重复数据?
Duplicate data when hit send button after refreshing collection view in Swift 3?
我在使用 JSQMessage 库和 Firebase 的聊天应用程序中遇到了这个错误。当我刷新以在集合视图中加载更多消息后按回车键时消息重复?
加载更多功能有效,但点击时输入新消息重复。
我尝试删除数组中的所有元素并重新加载集合视图,但仍然无效。当点击发送按钮时也重新加载集合视图但出现相同的错误。
我发现拉动刷新集合视图时出现错误。刷新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()
}
}
}
}
})
}
}
这是我的错误的图像。因为我没有足够的声望所以请点击link查看图片https://i.stack.imgur.com/J1ir3.jpg
我找到了我自己问题的解决方案。只需将 remove all observe 添加到 loadMore()
func.
的顶部
databaseReference.child("Conversation").child(fromId).child(toId).removeAllObservers()
我在使用 JSQMessage 库和 Firebase 的聊天应用程序中遇到了这个错误。当我刷新以在集合视图中加载更多消息后按回车键时消息重复?
加载更多功能有效,但点击时输入新消息重复。
我尝试删除数组中的所有元素并重新加载集合视图,但仍然无效。当点击发送按钮时也重新加载集合视图但出现相同的错误。
我发现拉动刷新集合视图时出现错误。刷新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()
}
}
}
}
})
}
}
这是我的错误的图像。因为我没有足够的声望所以请点击link查看图片https://i.stack.imgur.com/J1ir3.jpg
我找到了我自己问题的解决方案。只需将 remove all observe 添加到 loadMore()
func.
databaseReference.child("Conversation").child(fromId).child(toId).removeAllObservers()