用firebase完成处理程序不起作用

发布于 2025-02-12 13:18:55 字数 1047 浏览 0 评论 0原文

我尝试从Firebase获取值,然后我想打印执行第二行的代码,然后是TRUE。

但是订单是完全错误的。我在做什么错?

override func viewDidLoad() {
        super.viewDidLoad()
        

        method(arg: true, completion: { (success) -> Void in
            print("Second line of code executed")
            if success { // this will be equal to whatever value is set in this method call
                  print("true")
            } else {
                 print("false")
            }
        })
        
    }
    func method(arg: Bool, completion: (Bool) -> ()) {
        let userID = Auth.auth().currentUser!.uid
        let database = Database.database().reference()
        database.child("user/\(userID)/abonniertePoi/").observe(.value, with: { snapshot in
            for child in snapshot.children.allObjects as! [DataSnapshot] {
                myFeed.myArray1.append(child.key)
            }
            print(myFeed.myArray1)
            
        })
        completion(arg)
    }

它首先打印第二行执行,然后执行firebase请求,然后说为true。

I try to get the values from Firebase and after that I want to print the second line of code executed and then true.

But the order is total wrong. What am I doing wrong?

override func viewDidLoad() {
        super.viewDidLoad()
        

        method(arg: true, completion: { (success) -> Void in
            print("Second line of code executed")
            if success { // this will be equal to whatever value is set in this method call
                  print("true")
            } else {
                 print("false")
            }
        })
        
    }
    func method(arg: Bool, completion: (Bool) -> ()) {
        let userID = Auth.auth().currentUser!.uid
        let database = Database.database().reference()
        database.child("user/\(userID)/abonniertePoi/").observe(.value, with: { snapshot in
            for child in snapshot.children.allObjects as! [DataSnapshot] {
                myFeed.myArray1.append(child.key)
            }
            print(myFeed.myArray1)
            
        })
        completion(arg)
    }

It first prints second line executed then does the Firebase request and then says true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文