1

I am trying to get a table view to update from my Firebase database. My data is structured like this (for reporting automobiles):

  1. Reports:
    • Randomly generated report ID number:
      • make: "make of car"
      • model: "make of model"

I don't think that I am calling the data correctly. I do not know how to select for the random report ID. But there may be something else that I am missing. I am trying to get only the make and model of the vehicle to display in the text of the cell

var reportList:[String] = [] var ref: DatabaseReference! var handle: DatabaseHandle? @IBOutlet weak var reportsTableView: UITableView! @IBAction func backButtonPressed(_ sender: UIBarButtonItem) { self.performSegue(withIdentifier: "reportsToHome", sender: self) } public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return reportList.count } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = reportList[indexPath.row] return cell } override func viewDidLoad() { super.viewDidLoad() ref = Database.database().reference() handle = ref.child("Reports").observe(.childAdded, with: { (snapshot) in if (snapshot.value as? String) != nil { let make = String(describing: self.ref.child("Reports").child("make")) let model = String(describing: self.ref.child("Reports").child("model")) self.reportList.append(make + model) self.reportsTableView.reloadData() } } // Do any additional setup after loading the view. )} override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

}

2
  • Are you trying to get multiple Reports, or will there only be the one Report? Commented Aug 1, 2017 at 14:36
  • There will be multiple reports. The database checks for any new reports coming in and posts them. Or at least it is supposed to. Commented Aug 1, 2017 at 15:04

1 Answer 1

2

I'm not able to test this, but I have feeling that you were trying to cast the snapshot as a string. Instead, you should cast it as a Dictionary so you can easily retrieve the data by key.

Try this code. It sets the snapshot as a dictionary, and from there you are able to retrieve the make and model:

override func viewDidLoad() { super.viewDidLoad() ref = Database.database().reference() handle = ref.child("Reports").observe(.childAdded, with: { (snapshot) in if let reports = snapshot.value as? NSDictionary { var make = reports?["make"] as? String var model = reports?["model"] as? String self.reportList.append(make + model) self.reportsTableView.reloadData() } } )} 

Going further, you can create a Report class:

class Report: NSObject { var make: String? var model: String? } 

You can then set the make and model from the snapshot to create a new Report object.

var make = reports?["make"] as? String var model = reports?["model"] as? String let newReport = Report() newReport.setValuesForKeys(reports) 

I hope this works, if not I'll look again.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it, but it did not work. I will play around with the Report class in the mean time. EDIT: never mind. It works, I had accidentally deleted too much when editing. Thank you very much for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.