简体   繁体   中英

Issue when fetching random documents from Firebase Firestore with Swift on iOS

I want to fetch random documents from Firestore every time the user opens my application.

I implemented the functionality correctly and It fetches documents randomly as required.

My problem is that when for instance the random document is the last document in my Firestore database. It would stop loading new documents as there are no more documents after it. Because I have implemented also the load next documents when the user reaches the bottom of the page.

How can I solve this issue?

Please any help on how to fix this issue? I want to load random documents without it stopping a load of documents when it reaches the final document before it should.

Here is my code:

    func loadData(){
        
        let randomShops = db.collection("Shops");
        key = randomShops.document().documentID
          
        db.collection("Shops").whereField("__name__", isGreaterThanOrEqualTo: self.key!).order(by: "__name__", descending: true).limit(to: 8).getDocuments(){
            querySnapshot, error in
            if let error = error {
                print(error.localizedDescription)
            }else {
                self.lastDoc = querySnapshot?.documents[(querySnapshot?.documents.endIndex)! - 1]
                self.shops = querySnapshot!.documents.compactMap({addShop(dictionary: $0.data())})
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
          
        }
 }

Then I would fetch new documents as the user scrolls down. but if the document that appears is the last document in my Firestore database it wouldn't load more documents even though not all the documents have been loaded.

    func getNextShops() {

        db.collection("Shops").order(by: "__name__").start(afterDocument: lastDoc!).limit(to: 8).getDocuments(){ docs, err in
            self.docsCount = docs?.count
            if  self.docsCount == 0{
                self.tableView.stopLoading()
                return
            }else{
            if let err = err {
                print(err.localizedDescription)
            }else{
                    docs?.documents.forEach{ data in
                       
                            self.shops.append(addShop(dictionary: data.data())!)
                            DispatchQueue.main.async {
                                self.tableView.reloadData()
                                
                            }
                    }
                self.lastDoc = docs?.documents[(docs?.documents.endIndex)! - 1]
                self.lastDocID = self.lastDoc?.documentID
        
                }
            }
        }
}

If the issue is not getting enough random documents, Simply running the request again and appending the results should meet the same criteria. Most random solutions for Firestore are only made for 1 random float, I suggest adding a second for additional requests in this situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM