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

[ad_1]

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.

Code

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 {
                print("Error..(error.localizedDescription)")
            } else if snapshot!.isEmpty {
                self.fetchMoreDocs = false
                return
            } else {
                let newMessages = Message.parseData(snapshot: snapshot)
                self.messages.append(contentsOf: newMessages)
                DispatchQueue.important.async {
                    self.chatRoomTV.reloadData()
                    self.scrollToBottom()
                    self.fetchMoreDocs = false
                }
            }
            self.lastDocument = snapshot!.paperwork.final
        }
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let place = scrollView.contentOffset.y
        let maxOffset = scrollView.contentSize.top - scrollView.body.dimension.top
        if maxOffset - place <= 50 {
            print("Fetch older messages")
            fetchChatMessages()
        }}

[ad_2]

Leave a Reply