您好我是swift编程的新手,我想从loadData()获取标签值,以使用我的路径(引用)到我的数据库dbRef!.child(place“/ placeLabel”).我的意思是?!我读取“dio Con dio”节点上的数据.发生这种情况是因为价值所在的位置=“dio Con dio”.因此,我的应用程序不会从“Paradosiako – Panorama”节点加载数据.我想从数据库加载所有内容,我认为如果我可以创建位置值以更改到下一个节点,它可以读取所有数据.
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() } } }
@L_301_0@
(这个问题是跟进from this one.)
对于新的数据库架构,请尝试以下方法:
原文链接:https://www.f2er.com/swift/319955.htmlprivate 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) } } }