【问题标题】:Check if user already exists Firebase swift 3.0检查用户是否已经存在 Firebase swift 3.0
【发布时间】:2017-03-13 14:57:45
【问题描述】:
let database = FIRDatabase.database().reference()
    database.child("Users").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
        print(snapshot)
        if let value = snapshot.value as? [String: AnyObject] {
            let ui = value["id"] as! String
            if ui != FIRAuth.auth()!.currentUser!.uid
            {
                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                if s == true
                {
                    storyboard = UIStoryboard(name: "Main", bundle: nil)
                }
                else if p == true
                {
                    storyboard = UIStoryboard(name: "big", bundle: nil)
                }
                else if se == true
                {
                    storyboard = UIStoryboard(name: "se", bundle: nil)
                }
                else if os == true
                {
                    storyboard = UIStoryboard(name: "4s", bundle: nil)
                }
                else if ip1 == true
                {
                    storyboard = UIStoryboard(name: "ipad1", bundle: nil)
                }
                else if ipb == true
                {
                    storyboard = UIStoryboard(name: "ipadbig", bundle: nil)
                }

                        let naviVC = storyboard.instantiateViewController(withIdentifier: "eula")as! UIViewController
                        let appDelegate = UIApplication.shared.delegate as! AppDelegate
                        appDelegate.window?.rootViewController = naviVC

            }
            else
            {
                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                if s == true
                {
                    storyboard = UIStoryboard(name: "Main", bundle: nil)
                }
                else if p == true{
                    storyboard = UIStoryboard(name: "big", bundle: nil)
                }
                else if se == true
                {
                    storyboard = UIStoryboard(name: "se", bundle: nil)
                }
                else if os == true{
                    storyboard = UIStoryboard(name: "4s", bundle: nil)
                }
                else if ip1 == true
                {
                    storyboard = UIStoryboard(name: "ipad1", bundle: nil)
                }
                else if ipb == true
                {
                    storyboard = UIStoryboard(name: "ipadbig", bundle: nil)
                }
                let naviVC = storyboard.instantiateViewController(withIdentifier: "yo")as! UIViewController
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.window?.rootViewController = naviVC
            }
        }


    })

我有一个 firebase 应用程序,我想检查我的数据库中是否存在用户,如果我不想显示许可协议,但如果有,我也没有。我当前的方法只有在有两个用户时才有效。提前谢谢你的帮助。

【问题讨论】:

    标签: swift firebase firebase-realtime-database firebase-authentication eula


    【解决方案1】:

    我想通了

      let database = FIRDatabase.database().reference()
        database.child("Users").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
            print(snapshot)
    
            if snapshot.hasChild( FIRAuth.auth()!.currentUser!.uid)
                {
    

    【讨论】:

      【解决方案2】:

      您需要做的就是将 .ChildAdded 更改为 .value。这将确保它在您启动时读取数据。

      【讨论】:

        【解决方案3】:

        使用该代码,我认为它只会在添加孩子时执行。我现在无法检查,但请尝试使用

        database.child("Users").queryOrderedByKey().observe(.Value, with:{ (snapshot) in
                    ...
            })
        

        正如我所说,我无法检查它,所以如果您有任何问题,请回复我。祝你好运!

        【讨论】:

        • @AtaerCaner .Value 让我的应用崩溃
        • 是的,我记不太清了,但在你的情况下它必须是 .Value。
        • 是的,我在发布后就意识到它必须是 .value
        • 现在也是小写的v
        • 而且你不需要 FIRDataEventType,你可以使用:observeSingleEvent(of: .value, with: { (snapshot) in
        猜你喜欢
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多