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


    })

I have a firebase app, and I want to check if a user exists in my databse, If the don't i want to display a licenses agreement, but if they do i don't have too. My current method only works if there are two users. Thank you for your help in advance.

Arnav Chawla
  • 444
  • 3
  • 15

3 Answers3

1

I figured it out

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

        if snapshot.hasChild( FIRAuth.auth()!.currentUser!.uid)
            {
Arnav Chawla
  • 444
  • 3
  • 15
1

All you need to do is change .ChildAdded to .value. This will make sure its reading the data when you start.

0

With that code I think it will only execute when a child is added. I can't check it out right now but try with

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

As I said I can't check it so answer me if you have any problem. Good luck!

AtaerCaner
  • 691
  • 7
  • 12
Jaime Alcántara Arnela
  • 2,062
  • 5
  • 25
  • 56