【问题标题】:Make AuthListener wait until for loop has finished让 AuthListener 等到 for 循环完成
【发布时间】:2019-05-18 21:53:35
【问题描述】:

我在每次登录时读取用户的数据。因为我在 AppDelegate.swift 中有一个 Firebase AuthListener,所以一旦用户登录,视图就会改变。

问题是,在某些情况下,视图的变化比数据处理的速度更快,这会导致屏幕没有所需的数据。

过去我通过在更改视图的代码中添加:databaseRef.observeSingleEvent(of: .value, with: { snap in ... }) 来绕过这个:

if user != nil && currentCar == "auto auswählen" {
                databaseRef.observeSingleEvent(of: .value, with: { snap in
                    if user?.isAnonymous == false {
                        let controller = storyboard.instantiateViewController(withIdentifier: "RootPageViewController") as! RootPageViewController
                        let vc = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
                        vc.readTimeline()
                        self.window?.rootViewController = controller
                        self.window?.makeKeyAndVisible()
                    } else if user?.isAnonymous == true {
                        let controller = storyboard.instantiateViewController(withIdentifier: "RootPageViewController") as! RootPageViewController
                        self.window?.rootViewController = controller
                        self.window?.makeKeyAndVisible()
                    }
})

这很好,但是现在我在 for 循环的帮助下读取了我的数据,它不会因为数据库请求比 for 循环更早完成:

databaseRef.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary

            for n in 1...snapshot.childrenCount {
                databaseRef.child("users/\(uid)").child(String(n)).observeSingleEvent(of: .value, with: { (snapshot) in
                    let value = snapshot.value as? NSDictionary

                    ...

                }) { (error) in
                    print(error.localizedDescription)
                }
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }

如何让 AppDelegate.swift 和 AuthListener 等到 for 循环完成?

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    您希望等待所有数据加载完毕后再转到新视图。一种方法是计算已经加载了多少子节点,然后在加载完所有子节点后继续。

    在看起来像这样的代码中:

    databaseRef.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
        let value = snapshot.value as? NSDictionary
        let loadedCount = 0
    
        for n in 1...snapshot.childrenCount {
            databaseRef.child("users/\(uid)").child(String(n)).observeSingleEvent(of: .value, with: { (snapshot) in
                loadedCount = loadedCount + 1
                let value = snapshot.value as? NSDictionary
    
                ...
    
                if (loadedCount == snapshot.childrenCount) {
                   // TODO: all data is loaded, move to the next view
                }
            }) { (error) in
                print(error.localizedDescription)
            }
        }
    }) { (error) in
        print(error.localizedDescription)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 2014-07-19
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2021-08-09
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多