0
votes

I have a tableViewController that fetch document from Firestore collection which called "paperHole" and i have refreshControl which is a UIrefreshControl that refresh the tableView but in this refreshControl action i want to fetch the last document only that recorded in the Firestore . what i mean is if the collection "paperHole" have 3 documents which is ( A, B, C ) and the last document recorded in this collection is C , i want to fetch the document C only . i tried with limit(toLast: 1) but it didn't work for me

import UIKit
import FirebaseFirestore
import Firebase
import FirebaseAuth
import UserNotifications


class OrderTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {




@IBOutlet var order5: UITableView!

struct GlobalVariable{
    static var myString = String()
}
var db: Firestore!
var street = [String]()
var firstName = [String]()
var lastName = [String]()
var blockNumber = [String]()
var phone = [String]()
var reciept = [String]()
var houseNumber = [String]()
var price = [String]()
var amount = [String]()
var block = [String]()
var Area = [String]()
var names = [String]()
var refreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    
    refreshControl.attributedTitle = NSAttributedString(string: "")
    refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
    order5.addSubview(refreshControl)  
    order5.separatorStyle = .none
    order5.dataSource = self
    order5.delegate = self
    
    db = Firestore.firestore()
    
    
    
    
    
    
}


@objc func refresh(_ sender: AnyObject) {
    
    loadData1Refresh()
}


func loadData1Refresh() {
    
    
    Firestore.firestore().collection("paperHole").limit(toLast: 1).getDocuments() {
        (querySnapshot, err) in
        
        if let err = err
        {
            print("Error getting documents: \(err)");
        }
        else
        {
            
            var count = 0
            for document in querySnapshot!.documents {
                count += 1
                print("\(document.documentID) => \(document.data())");
                
                self.firstName.append(document.get("firstname") as? String ?? "")
                self.lastName.append(document.get("lastname") as? String ?? "")
                self.street.append(document.get("street") as? String ?? "")
                self.blockNumber.append(document.get("blockNumber") as? String ?? "")
                self.Area.append(document.get("area") as? String ?? "")
                self.phone.append(document.get("phone") as? String ?? "")
                self.reciept.append(document.get("reciept") as? String ?? "")
                self.houseNumber.append(document.get("houseNumber") as? String ?? "")
                self.price.append(document.get("total price") as? String ?? "")
                self.amount.append(document.get("amount") as? String ?? "")
                
                
            }
            
            
        }
        
        self.order5.reloadData()
        
    }
    
}
1

1 Answers

0
votes

I believe that using limit won't work unless you specify an order too.

Firstly, you'll need to attach a date to your paperHole object. As per the Firebase Docs:

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

Then you can query your data using:

Firestore.firestore().collection("paperHole").order(by: "date", descending: true).limit(to: 1)