ios - Swift 3 中 Firebase 同步性不起作用的解决方法

标签 ios swift asynchronous firebase firebase-realtime-database

我试图强制某些函数在使用 Firebase 之前完成从它获取信息。虽然我一直在阅读有关调度组和完成处理的内容,但我不明白为什么仅执行以下操作不起作用:

while (self.expectedPointCount == -1) {
    self.ref?.child("Users").child(self.userID!).child("presetRouteCoordinateCount").observeSingleEvent(of: .value, with:{
            snapshot in
            self.expectedPointCount = snapshot.value as! Int //always zero or greater

    })
        while(self.expectedPointCount == -1){}
}

这将永远运行。

在 while 循环和 Firebase 快照例程中插入打印语句表明它实际上从未进入 Firebase 快照例程。然而,当它不在 while 循环中时,它确实会进入例程。

看起来我必须以艰难的方式去做,但我就是不明白为什么。

非常感谢!

最佳答案

这将无限循环。尽管 Firebase 调用是在不同的线程上异步执行的,但它永远没有机会启动,因为您永远不会在调用的线程上留下无限 while 循环。

关于ios - Swift 3 中 Firebase 同步性不起作用的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43082532/

相关文章:

ios - 应用程序组和 UIImage Swift

ios - 长链接不可解析 - iOS 上的 Firebase 动态链接

ios - 如何使用 UIActivityViewController 共享经度和纬度

javascript - 从函数返回的数据未定义

asynchronous - 如何链接 ObservableObject?

javascript - Promise.then 在前一个 .then 和 foreach 完成之前被解析

ios - 验证登录表单,我正在使用 NSString?

ios - 是否可以在一个 XIB 中有两个 TableView ?

swift - 如何将数组值从 ViewController 的协议(protocol)传递到另一个 ViewController

swift - UIScrollView 宽度比屏幕宽