Max
Max

Reputation: 1

load only new data from Firestore collection

I have a tableViewController and its fetch the data from Firestore, i have a refreshController in this tableView what i want is to reload the new data from Firestore , if there is a new data in the Firestore and the user refresh the tableView i want to retrieve the new data ( only ) not all the data . i tried with reloadData() but it reload all the data and add it again in the tableView

this is my code

import UIKit
import FirebaseFirestore
import Firebase
import FirebaseAuth
import UserNotifications


class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {


@IBOutlet var tableView: UITableView!
   


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)
      tableView.addSubview(refreshControl) // not required when using UITableViewController

    
    tableView.register(UINib(nibName: "TableViewCell", bundle: nil) , forCellReuseIdentifier: "Cell")
    
    tableView.separatorStyle = .none
    tableView.dataSource = self
    tableView.delegate = self
    
    db = Firestore.firestore()
    self.navigationItem.setHidesBackButton(true, animated: true)
    

    NotificationCenter.default.addObserver(self, selector: #selector(setter: tableView), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
    

}


   @objc func refresh(_ sender: AnyObject) {
      tableView.reloadData()
      refreshControl.endRefreshing()

     
  }



func loadData1() {
   
    
    Firestore.firestore().collection("hola").getDocuments() { [self]
            
            (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.tableView.reloadData()
            
        }
        
        
  } 

Upvotes: 0

Views: 388

Answers (1)

Sergei Sevriugin
Sergei Sevriugin

Reputation: 498

I think the idea will be to use timestamp in your Firestore document and query all documents with the timestamp value greater that the last record you have extracted. You could store timestamp in UserDefaults if needed.

Upvotes: 1

Related Questions