Firestore Database collection count to show in Tableview

Hello,

The code below shows the number of items in a firebase data collection “messenger”. I have been trying to get this count to be returned as the count for rows in a tableview. I have’t had success yet it as it does’t show everything. it always show 3 of the five rows and I would like for them to be arranged according to date it was posted. I keep getting errors like “Thread 1: Fatal error: Index out of range” Please help!

 // get the count of collections

    func getCollections() {

        let db = Firestore.firestore()

        db.collection("messenger").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())");
                }
                print("Count = \(count)");


            }
        }
    }

You need to load the data from Firebase into an array and that array is then the data source for the tableView.

2 Likes

Is there a tutorial that can show me how to do this? When I attempt it I only get one row when in the above code, it show count = 7. and yes in fact there are 7 entries.

Unless you know a better way to get the accurate rows to display, my thought is, how might I get the count in the above method to be returned in this below code which is the “sections” part of the UITableView. I believe if this is figured out, I will have the results that I need to both display the count on the menu button and show a Firebase collection in it’s fullness:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Connect indexpath.row to Firebase Database
       
        return count
        }

I keep getting this error:

Thread 1: Fatal error: Index out of range

from this:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Get count to return accurate indbexpath.rows from Firebase
        var collecCount = items[db.collection("messenger").collectionID.count]!
        return collecCount

As I was saying in my previous post, the tableView relies on an array of data.

In the tableView method numberOfRowsInSection it needs the array.count to provide the number of rows that it expects to have to create.

In the most basic terms of a tableView example, let’s say we have an array like this:

var phoneticAlphabet = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "Indigo", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whisky", "Xray", "Yankee", "Zulu"]

So in the tableView numberOfRowsInSection we would say this:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return phoneticAlphabet.count
}

Then in the cellForRowAt method we might do something like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") {
        let item = phoneticAlphabet[indexPath.row]
        cell.textLabel?.text = item
        return cell
    }
    // If it fails to dequeue a cell
    return UITableViewCell()
}
1 Like

What if my array is Infinite? What I am making is kind of like internal email system, where my users can get memos/bulletins. So posts might keep increasing in number. How do I avoid “Thread 1: Fatal error: Index out of range”? I think it is trying find a limit to which I don’t think there should be one. Also, it must be filtered according to date & time.

That doesn’t matter since if the data in the array increases then the count adjusts and the new rows are added.

If you have a “listener” method in Firebase which is retrieving records from that collection, when a change occurs the listener updates the array and the table adjusts accordingly. That goes for where records are either deleted or added.

1 Like

I should add for clarification that when the array is updated a call needs to be made on tableView to reloadData and that is done with the code:

tableView.reloadData()

1 Like