我有一个Firebase数据库,如下所示:
保存到数据库没有问题,但是当我尝试按以下方式检索数据时:
//Properties
@IBOutlet weak var bike1LBL: UILabel!
@IBOutlet weak var bike2LBL: UILabel!
@IBOutlet weak var bike3LBL: UILabel!
@IBOutlet weak var bike4LBL: UILabel!
@IBOutlet weak var chainLBL: UILabel!
@IBOutlet weak var brakeLBL: UILabel!
@IBOutlet weak var locationLBL: UILabel!
@IBOutlet weak var phoneLBL: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var ref = Firebase(url:"sdfghjkl/firebaseio.com/Order/")
// Retrieve new posts as they are added to database
ref.observeEventType(.ChildAdded, withBlock: { snapshot in
let road = snapshot.value.objectForKey("Bike-Type1") as? String
self.bike1LBL.text = road
let city = snapshot.value.objectForKey("Bike-Type2") as? String
self.bike2LBL.text = city
let mountain = snapshot.value.objectForKey("Bike-Type3") as? String
self.bike3LBL.text = mountain
let christiania = snapshot.value.objectForKey("Bike-Type4") as? String
self.bike4LBL.text = christiania
let chain = snapshot.value.objectForKey("Chain") as? String
self.chainLBL.text = chain
let brake = snapshot.value.objectForKey("Brake") as? String
self.brakeLBL.text = brake
let location = snapshot.value.objectForKey("Location") as? String
self.locationLBL.text = location
let phone = snapshot.value.objectForKey("Phone") as? String
self.phoneLBL.text = phone
print(snapshot.value)
})
}
应用程序崩溃,我收到此错误消息,但这是我从调试器获得的所有内容:
答案 0 :(得分:0)
如果您更新到新的Firebase 3.0 api并查看MakinGIANTS - Post关于firebase和查询(它在Kotlin中,但“看起来像”Swift)并且可能有所帮助
会更好答案 1 :(得分:0)
您可以使用FIRDataEventTypeValue事件来读取数据 给定路径,因为它在事件发生时存在。这个方法是 每当听众被连接时再次触发一次 数据,包括任何儿童,都会发生变化事件回调传递a 包含该位置所有数据的快照,包括子数据。
所以snapshot
实际上是Order
中某些对象的数组,而不仅仅是最近添加的对象。尝试获取最后一个对象,然后从中获取数据:
ref.observeEventType(.ChildAdded, withBlock: { snapshot in
let entry = (snapshot as! NSArray).lastObject as! NSDictionary
let road = entry.objectForKey("Bike-Type1") as? String
self.bike1LBL.text = road
})