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

Check if user already exists Firebase swift 3.0

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

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

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

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

我想通了

  let database = FIRDatabase.database().reference()
    database.child("Users").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
        print(snapshot)

        if snapshot.hasChild( FIRAuth.auth()!.currentUser!.uid)
            {

您只需将 .ChildAdded 更改为 .value。这将确保它在您开始时读取数据。