【发布时间】:2016-06-03 01:28:53
【问题描述】:
我正在尝试从 firebase 数据库中检索数据。但是,我无法将本地变量分配给数据库的值。我正在使用以下类和方法。
class Episode {
var title: String?
var description: String?
var location: String?
var discount: String?
var star: Int?
init() {
self.title = ""
self.description = ""
self.location = ""
self.discount = ""
self.star = 0
}
这是我从数据库中提取数据的方法
func getValues() -> Episode {
let rootRef = FIRDatabase.database().reference().child("Restaurants").child("The Kafe")
let descriptionRef = rootRef.child("Description")
let discountRef = rootRef.child("Discount")
let locationRef = rootRef.child("Location")
let starRef = rootRef.child("Star")
let episode = Episode()
descriptionRef.observeEventType(.Value) { (snap: FIRDataSnapshot) in
episode.description = snap.value as? String
}
discountRef.observeEventType(.Value) { (snap: FIRDataSnapshot) in
episode.discount = snap.value as? String
}
locationRef.observeEventType(.Value) { (snap: FIRDataSnapshot) in
episode.location = snap.value as? String
}
starRef.observeEventType(.Value) { (snap: FIRDataSnapshot) in
episode.star = snap.value as? Int
print(episode.description!)
}
return episode
}
当我打印出返回剧集的值时,它们都是空的。但是,当我在闭包本身中打印值时(例如,如果我在 obserEventType 闭包中执行 print(episode.description),它工作正常。但如果我在外面打印它是空的。
我认为我缺少关于 swift 或 firebase 的一些基本知识。我是 iOS 编程新手,因此我们将不胜感激。
【问题讨论】:
标签: ios swift firebase firebase-realtime-database