'Load only currently logged-in user data in cells. (Firebase)

I have an app with a SignUp/LogIn authentication created with Firebase. When the user is connected, he can add a cell and fill in two text fields. Then the cell data like the date, the two fields filled by the user and the email of the user who created the cell are saved with Firestore. I want that when a user logs in, only the cells that this user has created are displayed. How I can do it?

App image

Here is my FStore struct :

struct FStore {
    static let collectionName = "cells"
    static let firstText = "firstText"
    static let secondText = "secondText"
    static let senders = "sender"
    static let dateField = "date"
}

Here I save the cell informations :

        let firstBody = alert.textFields![0] as UITextField
        let secondBody = alert.textFields![1] as UITextField
        
        if firstBody.text != "" && secondBody.text != "" {
        
            if let cellSender = Auth.auth().currentUser?.email {
                
                self.db.collection(Constants.FStore.collectionName).addDocument(data: [Constants.FStore.senders: cellSender, Constants.FStore.firstText: firstBody.text!, Constants.FStore.secondText: secondBody.text!, Constants.FStore.dateField: Date().timeIntervalSince1970]) { error in
                    if let e = error {
                        print("Issue saving data to Firestore \(e)")
                    } else {
                        
                        print("succesfully saved")
                    }
                }
                
            }

CELLS struct :

struct CELLS {

let sender: String
let firstText: String
let secondText: String
}

Load Cell function (CellArray = [CELLS]) :

  func loadCells() {
   CellArray = []
    db.collection(Constants.FStore.collectionName).order(by: Constants.FStore.dateField).addSnapshotListener { querySnapshot, error in
        
        self.CellArray = []
        
        if let e = error {
            print("issue retrieving data \(e)")
        }else {
            if let snapshotDocuments = querySnapshot?.documents {
                for doc in snapshotDocuments {
                let data = doc.data()
                    if let sender = data[Constants.FStore.senders] as? String, let cellFirst = data[Constants.FStore.firstText] as? String, let cellSecond = data[Constants.FStore.secondText] as? String {
                        
                        let newCell = CELLS(sender: sender, firstText: cellFirst, secondText: cellSecond)
                        self.CellArray.append(newCell)
                        DispatchQueue.main.async {
                            self.tableView.reloadData()
                        }
                    }
                }
            }
        }
    }
}

And now I want to return only the user cells here :

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    // Return only the currently logged-in user cells
    return CellArray.count
    
}

Thanks for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source