我试图从firebase中检索数据并将其放到textviews中,但我所拥有的代码只提供了“history”中所有键的一个实例。
我想得到数据,如果“骑手”等于当前骑手id。
我在这里和互联网上尝试了不同的解决方案,但似乎什么也没做我需要做的事情。
Firebase数据库:
我的密码是:
let rider = FIRAuth.auth()?.currentUser?.displayName
// getting a reference to the node history
historyRef = ref.child("history")
// retrieve history key from firebase ....
let query = historyRef.queryOrdered(byChild: "rider").queryEqual(toValue: uid)
query.observe(.childAdded) { (snapshot) in
// history auto generated key ............
_ = snapshot.value as? String
let key = snapshot.key
// get values from history and place in outlet
self.historyRef.child(key).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
var dict = snapshot.value as! [String: AnyObject]
self.price = ((dict["ride_price"] as AnyObject) as! Double)
self.txtPrice1.text = "\(self.price!)" // to make double work in textview
self.txtPrice1.text = "\( Double(round(100 * self.price!)/100) )" // format .xx
self.txtPrice2.text = "\( Double(round(100 * self.price!)/100) )"
self.txtPrice3.text = "\( Double(round(100 * self.price!)/100) )"
self.distance = ((dict["distance"] as AnyObject) as! Double)
self.txtDistance.text = "\(self.distance!)"
self.txtDistance.text = "\( Double(round(100 * self.distance!)/100) )"
self.location = (dict["location"] as! String)
self.txtLocation.text = self.location
self.destination = (dict["destination"] as! String)
self.txtDestination.text = self.destination
self.timestamp = (dict["timestamp"] as! String)
self.txtTimestamp.text = self.timestamp
}
})
}
最佳答案
在@Ratul Sharker的帮助下,我的问题得到了解决。这是我们必须做的——把钥匙从历史传递到历史细节。
TripHistory公司
在我的上一个屏幕中,它包含指向所讨论页面的tableview
private var selectedIndexPath: IndexPath?
// didSelectRowAt indexPath
selectedIndexPath = indexPath
performSegue(withIdentifier: "segueShowTripDetail", sender: self)
// add method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let key = completedTrips[selectedIndexPath!.row]
if segue.identifier == "segueShowTripDetail" {
let destination = segue.destination as! TripDetail
destination.key = key
}
}
三足鼎立
public var key: String! // container for the passed-in historyKey
// use passed-in 'key'
self.historyRef.child(self.key).observe(.value, with: { (snapshot) in
关于ios - 如何遍历包含当前骑手ID的每个键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54384550/