Search code examples
swiftcollectionview

Duplicate data when hit send button after refreshing collection view in Swift 3?


I got this bug in my Chat App using JSQMessage library and Firebase. The message duplicate when i hit enter after refreshing to load more messages in collection view ?

Load more function works but when hit enter the new message duplicate.

I try to remove all elements in array and reload the collection view but still not work. Also reload collection view when hit send button but got the same error.

i found the error appear when i pull to refresh the collection view. Refresh 1 time, duplicate 1, refresh 2 times, duplicate 2 times...

Anyone please help me for fix this. Thank for you helping.

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()
                        }
                    }
                }


            }
        })
    }
}

Here is the image of my bug. Because i do not have enough reputation so please click the link to see the image https://i.sstatic.net/J1ir3.jpg


Solution

  • I found the solution for my own question. Just add remove all observe to the top of loadMore() func.

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