ios - 如何在 Firebase observeEvent block iOS 中更新 UI

标签 ios swift user-interface firebase-realtime-database grand-central-dispatch

我在网上搜索过,但没有找到与我的问题相关的答案。希望有人能帮我解决这个问题。提前致谢! 这是一个 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 线程上触发。您无需通过调度队列,只需从回调中更新 UI。

关于ios - 如何在 Firebase observeEvent block iOS 中更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42909411/

相关文章:

iphone - 将 "for...in...break"循环转换为 "while"循环

ios - 使用 ScrollView 时状态栏的背景颜色变为白色

ios - 生成具有相同属性的多个 Sprite 节点

swift - 刷新步进值

iphone - 如何在分组的 UITableView 单元格上使用自定义背景?

c# - Wix 使用编辑控件

ios - 为什么用户没有被保存在安装类中?

ios - 展开可选的 JSON 元素使应用程序崩溃

ios - 动态移动(动画)UITextField

java - 模型- View -控制和 Swing GUI