检查用户是否已存在Firebase swift 3.0

时间:2017-03-13 14:57:46

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

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应用程序,我想检查我的数据库中是否存在用户,如果我不想显示许可协议,但如果他们这样做,我也不会。我当前的方法仅在有两个用户时才有效。提前谢谢你的帮助。

3 个答案:

答案 0 :(得分: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)
            {

答案 1 :(得分:1)

您需要做的就是将.ChildAdded改为.value。这将确保您在开始时读取数据。

答案 2 :(得分:0)

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

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

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