Getting let value outside function

Hello, I am new to fast programming and I want to get the label value from loadData() to use for my path (link) in my database on dbRef!.child(place+"/placeLabel") . What I mean?! I read the data that is on the "Dio Con Dio" node. This is because place is equal to let place = "Dio Con Dio" . Thus, my application does not load data from the "Paradosiako - Panorama" node. I want to load everything from the database, and I thought that if I could change the value of place to the next node, it could read all the data.

  import UIKit import FirebaseDatabase class PlacesTableViewController: UITableViewController { //MARK: Properties @IBOutlet weak var placesTableView: UITableView! //database reference var dbRef:FIRDatabaseReference? var places = [Places]() private var loadedLabels = [String: String]() private var loadedRatings = [String: Int]() //handler var handle:FIRDatabaseHandle? override func viewDidLoad() { super.viewDidLoad() dbRef = FIRDatabase.database().reference() // Loads data to cell. loadData() } private func loadData() { let place = "Dio Con Dio" dbRef!.child(place+"/placeLabel").observe(.childAdded, with: { (snapshot) in let label = snapshot.value as! String self.updatePlace(snapshot.key, label: label) }) dbRef!.child(place+"/rating").observe(.childAdded, with: { (snapshot) in let rating = snapshot.value as! Int self.updatePlace(snapshot.key, rating: rating) }) } private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil) { if let label = label { loadedLabels[key] = label } if let rating = rating { loadedRatings[key] = rating } guard let label = loadedLabels[key], let rating = loadedRatings[key] else { return } if let place = Places(name: label, rating: rating) { places.append(place) placesTableView.reloadData() } } } 

** My database **

(This question is a continuation of this .)

+2
let swift firebase firebase-database value
source share
2 answers

For your new database schema, try this instead:

 private func loadData() { dbRef!.observe(.childAdded) { (placeSnapshot) in print("Adding place \(placeSnapshot.key)...") // Load each label on the "placeLabel" list. let labels = placeSnapshot.childSnapshot(forPath: "placeLabel") for (key, label) in labels.value as! [String: String] { self.updatePlace(key, label: label) } // Load each rating in the "rating" list. let ratings = placeSnapshot.childSnapshot(forPath: "rating") for (key, rating) in ratings.value as! [String: Int] { self.updatePlace(key, rating: rating) } } } 

The updatePlace function simply groups the labels and rating properties of a given place, which are separately loaded by loadData .

Removing places . Of course, the above code will not handle the removal of space. To do this, you will also need to listen for the childRemoved event.

+2
source share

It's hard to explain this in a comment, so I will review your current problem here, but Paulo answered your question, I suppose. You can even open a new question on this issue.

Here is the code you are having trouble with:

 for (key, rating) in ratings.value as! [String: Int] { self.updatePlace(key, rating: rating) } 

There is nothing wrong with this code if the ratings.value value is filled in whole whole pairs. Your error occurs due to a missing key. (The runtime detected NSNull, so it could not convert the data to a dictionary)

You need to set a breakpoint at which you are building the array:

 placeSnapshot.childSnapshot(forPath: "rating") 

(which is called rating here / locally). If it’s normal to ignore keyless entries, skip them and do not add to your array. But most likely you have an error and you need to find out why you are adding a null value ...

It makes sense? Please accept Paulo's answer if he solves the problem, and open a new question with additional information about childSnapshot (code), and someone will definitely help you. (I know I will try.)

+3
source share

All Articles