Firebase data not displaying in tableview in xcode

Viewed 129

I have been trying to retrieve profile data from firebase realtime database and displaying it in a table view, so far when I run the simulator the tabelview shows up blank and Failed : error 0:50 [50] shows up. I am pretty new to swift and am not sure what the problem is.

Here is my code:

import UIKit
import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth

class userViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return profileData.count
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "firstName_cell",
                                                 for: indexPath)

        self.tableView.reloadData()
        cell.textLabel?.text = self.profileData[indexPath.item]
        return cell
   
    }
    
    
    
    var profileData = [String]()
    
    let storageRef = Storage.storage().reference()
    var databaseRef = Database.database().reference()
    var ref: DatabaseReference?
    @IBOutlet var tableView: UITableView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        ref = Database.database().reference()
        
        //let userID = Auth.auth().currentUser?.uid
        ref?.child("users").observeSingleEvent( of: .childAdded, with: { (snapshot) in
       
            let profile = snapshot.value as? String
            
            if let actualProfile = profile{
                self.profileData.append(actualProfile)
                print(self.profileData)
                self.tableView.reloadData()
            }
            
              }) { (error) in
                print(error.localizedDescription)
            }


            
        }
        
    }
2 Answers

Don’t add reloadData() in cellForRow method.... it will become infinite loop .. so remove self.tableView.reloadData() from cellForRow method

You try to reload your tableView while its reloading.

You should not call tableView.reloadData() in tableView(_:cellForRowAt:) method or any other UITableViewDataSource method that is called from same UITableView.

Related