如何在 Firebase observeEvent block iOS 中更新界面



我在网上搜索过,但没有找到与我的问题相关的答案。希望有人可以帮助我。提前感谢!下面是一个 Firebase observeEvent 函数。

ref.child("path").observeSingleEvent(of: .value, with: { (snapshot) in 
// here I have a for loop
for (_, dict) snapshot.value as! NSDictionary {
    // do something...
    //
    // here I would like to do some UI updating, like a progress bar, or just as simple as update the text in a label
    label.text = "an object fetched."
}
// I used to do some UI update here, and it works, like tableview.reloadData(), but this time I would like the UI updating happen in that for loop
}) { (error) in
    print(error.localizedDescription)
}

有人可以帮助我吗?我试过了

DispatchQueue.main.async {
 // UI update here but it doesn't work
}

希望有人能帮助我,非常感谢!

Firebase 事件回传会在主/界面线程上触发。无需通过调度队列,但只需从回调中更新 UI。

最新更新