【问题标题】:Code not finishing the function, it is ending execution halfway through代码没有完成功能,它正在中途结束执行
【发布时间】:2020-01-13 03:53:52
【问题描述】:

我的代码如下:

@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)
        }
    }

"in here" 被打印,但 bool 永远不会被打印并且警报正在显示。为什么我的代码没有进入 if bool 块并输出警报?

【问题讨论】:

  • 您正在调用 async 函数,其中您正在更改 bool 值,但您在该函数之外打印,您能检查一下是否正确?

标签: swift firebase firebase-realtime-database


【解决方案1】:

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

这解释了您所看到的行为:当您的 runs, the 尚未运行时。

解决方案很简单,因为它最初令人困惑和烦人:任何需要数据库数据的代码都必须在闭包内,或者从那里调用。

例如:

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)
    }
})

另见:

【讨论】:

  • 谢谢,密码正确,但是当我输入错误的密码时,它仍然把我带到下一页,我收到以下消息:Warning: Attempt to present <UIAlertController: 0x1060dfa00> on <OCRApp.ViewController: 0x105b06d50> which is already presenting <OCRApp.DatabaseTableViewController: 0x105af0a40>。我该如何解决这个问题?
【解决方案2】:

当您在登录后导航到下一个视图控制器时,您还必须将变量 bool 设置为 false。这样您再次登录,如果密码错误,则无法导航到下一页,只会显示密码错误警报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2015-08-11
    相关资源
    最近更新 更多