【问题标题】:How to update UI within Firebase observeEvent block iOS如何在 Firebase observeEvent 块 iOS 中更新 UI
【发布时间】:2017-03-20 16:50:18
【问题描述】:

我在网上搜索过,但没有找到与我的问题相关的答案。希望有人可以帮助我。提前致谢! 这是一个 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 数据库实现进度条动画。
  • 找到了一个可行的解决方案here 看看。

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


【解决方案1】:

Firebase 事件回调在主/UI 线程上触发。您无需通过调度队列,只需在回调中更新 UI。

【讨论】:

  • 但我也尝试过像“label.text = "blablabla" "一样直接更新用户界面,但没有成功。
  • 如果我在 for 循环之后进行一些 UI 更新,它会起作用。但是现在我想要实现的是进度条之类的东西,假设它会循环 100 次,所以每个循环,我都想更新 UI,但它在 for 循环中不起作用跨度>
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多