ios – Swift unable to load chat messages not loading latest on the backside


I am engaged on this messaging app and I will fetch the primary group of messages, latest message on the backside utilizing: .order(by: TIMESTAMP, descending: false).restrict(toLast: 5), however, when I attempt to load the second group, the tableView isn’t populated. If I take advantage of .order(by: TIMESTAMP, descending: true).restrict(to: 5) for the primary name, it masses the messages however latest message on the prime and if i scroll, it masses the older messages with no points. Undecided find out how to make it work the opposite manner round. Any assistance is vastly appreciated. Right here is my code. Thanks.


func fetchChatMessages() {
        fetchMoreDocs = true
        var question: Question!
        if messages.isEmpty {
            question = MESSAGES_COLLECTION.doc(currentUID!).assortment(person.memberId).order(by: TIMESTAMP, descending: false).restrict(toLast: 5)
        } else {
            question = MESSAGES_COLLECTION.doc(currentUID!).assortment(person.memberId).order(by: TIMESTAMP, descending: false).begin(afterDocument: self.lastDocument!).restrict(to: 5)
        question.addSnapshotListener { (snapshot, error) in
            if let error = error {
            } else if snapshot!.isEmpty {
                self.fetchMoreDocs = false
            } else {
                let newMessages = Message.parseData(snapshot: snapshot)
                self.messages.append(contentsOf: newMessages)
                DispatchQueue.important.async {
                    self.fetchMoreDocs = false
            self.lastDocument = snapshot!
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let place = scrollView.contentOffset.y
        let maxOffset = -
        if maxOffset - place <= 50 {
            print("Fetch older messages")


Leave a Reply