代码未完成功能,它在执行中途结束 [英] Code not finishing the function, it is ending execution halfway through

查看:50
本文介绍了代码未完成功能,它在执行中途结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

@IBAction func clicked(_ sender: Any) {
        let ref = Database.database().reference()
        let pass = password.text
        var firpass = ""
        var bool = false;
        ref.child(name.text as! String).child("password").observeSingleEvent(of: .value, with: { dataSnapshot in
          firpass = dataSnapshot.value as! String
            if firpass == pass {
                bool = true
                print("in here")
            }
        })
        print(bool)
        if bool {
            self.sendname = name.text!
            let vc = DatabaseTableViewController(nibName: "DatabaseTableViewController", bundle: nil)
            vc.finalName = self.sendname
            navigationController?.pushViewController(vc, animated: true)
            performSegue(withIdentifier: "username", sender: self)
        } else {
            let alert = UIAlertController(title: "Error", message: "Incorrect username or password", preferredStyle: UIAlertController.Style.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }

在这里"被打印,但布尔不打印,并且警报显示.为什么我的代码没有输入if bool块并输出警报?

"in here" gets printed, but bool is never printed and the alert is showing. Why does my code not enter the if bool block and output the alert?

推荐答案

数据是从Firebase异步加载的,因为它可能需要一段时间.而不是让您的应用程序等待数据(这将是糟糕的用户体验),而是在加载数据时继续执行主代码,然后在数据可用时调用闭包.

Data is loaded from Firebase asynchronously, since it may take a while. Instead of making your app wait for the data (which would be a bad user experience), your main code continues while the data is being loaded, and then once the data is available your closure is called.

这说明了您所看到的行为:到运行时,尚未运行.

This explains the behavior you're seeing: by the time your runs, the hasn't run yet.

该解决方案非常简单,因为它一开始就令人感到困惑和烦恼:需要数据库中数据的任何代码都必须在闭包内部,或者从那里进行调用.

the solution is as simple as it is initially confusing and annoying: any code that needs the data from the database must be inside the closure, or be called from there.

例如:

ref.child(name.text as! String).child("password").observeSingleEvent(of: .value, with: { dataSnapshot in
  firpass = dataSnapshot.value as! String
    if firpass == pass {
        bool = true
        print("in here")
    }
    print(bool)
    if bool {
        self.sendname = name.text!
        let vc = DatabaseTableViewController(nibName: "DatabaseTableViewController", bundle: nil)
        vc.finalName = self.sendname
        navigationController?.pushViewController(vc, animated: true)
        performSegue(withIdentifier: "username", sender: self)
    } else {
        let alert = UIAlertController(title: "Error", message: "Incorrect username or password", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
})

另请参阅:

  • Firebase with Swift 3 counting the number of children
  • Array of struct not updating outside the closure
  • getting data out of a closure that retrieves data from firebase (showing examples with custom callbacks and delegates)
  • How to reload data after all Firebase calls finished? (showing how to use a dispatch group)
  • Finish all asynchronous requests before loading data? (another example using a dispatch group)

这篇关于代码未完成功能,它在执行中途结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆