【问题标题】:Swift : not exiting Firebase observe fuction block [duplicate]Swift:不退出 Firebase 观察功能块 [重复]
【发布时间】:2016-10-25 04:19:40
【问题描述】:

不退出观察块。

不打印 hello 或 events。

卡在从数据库中获取。

显示来自 Firebase 但不在块外的数据。

例如:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let databaseRef = FIRDatabase.database().reference()
    databaseRef.child("Events").queryOrderedByKey().observe(.childAdded, with: {
        snapshot in
        print(snapshot)
        var temp : event = event()
        temp.name = (snapshot.value as? NSDictionary)?["name"] as? String ?? ""
        temp.description = (snapshot.value as? NSDictionary)?["description"] as? String ?? ""
        temp.date = (snapshot.value as? NSDictionary)?["date"] as? String ?? ""
        temp.price = (snapshot.value as? NSDictionary)?["price"] as? String ?? ""
        temp.venue = (snapshot.value as? NSDictionary)?["venue"] as? String ?? ""
        temp.genre1 = (snapshot.value as? NSDictionary)?["genre1"] as? String ?? ""
        temp.genre2 = (snapshot.value as? NSDictionary)?["genre2"] as? String ?? ""
        temp.img = (snapshot.value as? NSDictionary)?["img"] as? String ?? ""

        self.events.append(temp)
        print(self.events)
    })
    print("Hello")
    print(self.events)
}

提前致谢!

【问题讨论】:

标签: ios swift firebase firebase-realtime-database swift3


【解决方案1】:

这里有几件事:-

  • 您正在使用 childAdded 作为 FIRDataEventType,并且仅当您的 Events 节点获得值时才会触发此查询附加。如果您想访问数据库节点中的当前数据,请使用 value

    databaseRef.child("Events").queryOrderedByKey().observe(.value, with: {
    
  • print(self.events)completionBlock: 之外的语句将不起作用,因为对 firebase 数据库的制作是异步的,但只有在 completionBlock 中的 print(self.events) 语句才会起作用。

  • 如果您的 viewDidLoad 没有被调用,那么您当时可能不在正确的 viewController 上。

【讨论】:

    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2016-11-28
    • 2020-03-07
    • 1970-01-01
    • 2022-01-09
    • 2017-03-03
    • 2019-07-30
    相关资源
    最近更新 更多