Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning counting result from Firebase Query

I try to get the amount of rows in my Firebase.
But is says cannot find counter in scope on line return counter.

extension FourthTabFirstView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let ref = database.child("placeID")
        ref.observe(.value, with: { (snapshot: DataSnapshot!) in
        print(snapshot.childrenCount)
        let counter = snapshot.childrenCount
        })

        return counter
    }
    ......
    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1- Declare this instance variable

var counter = 0

2- This should be inside viewDidLoad

let ref = database.child("placeID")
ref.observe(.value, with: { [weak self] (snapshot: DataSnapshot!) in
   print(snapshot.childrenCount)
   self?.counter = snapshot.childrenCount
   self?.tableView.reloadData()
})

3- Change numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return counter
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading