7

I am trying to query Firebase to check if any user that has waiting: "1" and then when the snapshot is returned I want to see whether it is equal to nil. I have attempted to do this but the method I have used does not work and I only have some sort of out put if the snapshot is not equal to nil. I have added the code I currently have and the JSON text from Firebase.

import UIKit
import Firebase
import Spring

class GamesViewController: UIViewController {

let ref = Firebase(url: "https://123test123.firebaseio.com")
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()   

@IBAction func StartGamePressed(sender: AnyObject) {
    print("test1")
    var peopleWaiting: [String] = []

    let userRef = Firebase(url:"https://123test123.firebaseio.com/users")
    userRef.queryOrderedByChild("waiting").queryEqualToValue("1")
        .observeEventType(.ChildAdded, withBlock: { snapshot in
            print(snapshot.key)
            if snapshot.key == nil {
                print("test2")
                let userData = ["waiting": "1"]
                let usersRef = self.ref.childByAppendingPath("users")
                let hopperRef = usersRef.childByAppendingPath("\(self.ref.authData.uid)")

                hopperRef.updateChildValues(userData, withCompletionBlock: {
                    (error:NSError?, ref:Firebase!) in
                    if (error != nil) {
                        print("Data could not be saved.")
                        self.displayAlert("Oops!", message: "We have been unable to get you into a game, check you have an internet conection. If this problem carries on contect support")
                    } else {
                        print("Data saved successfully!")
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let Home : UIViewController = storyboard.instantiateViewControllerWithIdentifier("continueToGame")
                        self.presentViewController(Home, animated: true, completion: nil)

                    }

                })


            } else {
                var randomUID: String
                peopleWaiting.append(snapshot.key)
                let randomIndex = Int(arc4random_uniform(UInt32(peopleWaiting.count)))
                randomUID = peopleWaiting[randomIndex]
                print(randomUID)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let Home : UIViewController = storyboard.instantiateViewControllerWithIdentifier("continueToGame")
                self.presentViewController(Home, animated: true, completion: nil)

            }
        })
}

func displayAlert(title: String, message: String){

    let formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in

    })))

    self.presentViewController(formEmpty, animated: true, completion: nil)
}

func activityIndicatorFunction(){

    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 100, 100))
    activityIndicator.backgroundColor = UIColor(red:0.16, green:0.17, blue:0.21, alpha:1)
    activityIndicator.layer.cornerRadius = 6
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

}


}

JSON Data:

{
"68e42b7f-aea5-4c3f-b655-51a99cb05bb0" : {
  "email" : "test1@test1.com",
  "username" : "test1",
  "waiting" : "0"
},
"8503d5a8-fc4a-492b-9883-ec3664898b4f" : {
  "email" : "test2@test2.com",
  "username" : "test2",
  "waiting" : "0"
}
}
Tom Fox
  • 897
  • 3
  • 14
  • 34
  • Likely answer below. But please read about [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (because I think your question might have been "how do I detect if a certain child exists?") and about how to construct a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). If you take care of both, it'll be much easier for us to help you. – Frank van Puffelen Feb 29 '16 at 08:07

2 Answers2

20

There are a few things going on here, but the most important one is that you cannot test for the existence of children with .ChildAdded. That makes sense if you think about it: the .ChildAdded event is raised when a child is added to the location. If no child is added, the event won't be raised.

So if you want to test if a child exists at a location, you need to use .Value. Once you do that, there are various way to detect existence. Here's one:

ref.queryOrderedByChild("waiting").queryEqualToValue("1")
   .observeEventType(.Value, withBlock: { snapshot in
       print(snapshot.value)
       if !snapshot.exists() {
           print("test2")
       }
   });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4

Check for NSNull. This is the code for observing a node. Queries work much the same way.

Here's a complete and tested app. To use, change the string 'existing' to some path you know exists, like your users path and the 'notexisting' to some path that does not exist

    let myRootRef = Firebase(url:"https://your-app.firebaseio.com")
    let existingRef = myRootRef.childByAppendingPath("existing")
    let notExistingRef = myRootRef.childByAppendingPath("notexisting")

    existingRef.observeEventType(.Value, withBlock: { snapshot in

        if snapshot.value is NSNull {
            print("This path was null!")
        } else {
            print("This path exists")
        }

    })

    notExistingRef.observeEventType(.Value, withBlock: { snapshot in

        if snapshot.value is NSNull {
            print("This path was null!")
        } else {
            print("This path exists")
        }

    })

Please note that by using .Value, there will be a guaranteed a return result and the block will always fire. If your code used .ChildAdded then the block will only fire when a child exists.

Also, check to make sure of how your data appears in firebase.

users
   user_0
     waiting: 1

if different than

users
   user_0
     waiting: "1"

Note that "1" is not the same as 1.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • It does for sure work! I updated my answer with a complete and tested copy and pasted app I just wrote. If your code is not working then there is something else wrong - NSNull is the way to go! – Jay Feb 28 '16 at 14:09
  • Well all I know is that mine does not seem to be working, although the query you are using is different to mine... could that be making a difference? @Jay – Tom Fox Feb 28 '16 at 16:40
  • If your reference or query is malformed or the App is not set up correctly, it could cause all kinds of issues. I would suggest copy and pasting your code as a test method and remove everything but the bare necessities and step through your code to see where it's not working. – Jay Feb 28 '16 at 18:18
  • I know the query I am using is working fine because if there is a result for the query to return then it does and the code works perfectly, but if there is not nothing happens... @Jay – Tom Fox Feb 28 '16 at 18:32
  • @Jay "I believe that Firebase transactions could return a nil snapshot" We don't pass a snapshot into the transaction block, but the current value. That current value could indeed be null. But you're not using transactions here, so it doesn't make a difference. – Frank van Puffelen Feb 28 '16 at 18:34
  • I don't think `nil` vs `NSNull` is the important part here. The problem in OP is that it's using `.ChildAdded`, which will only fire *when* a child exists. Your approach using `.Value` is the correct one for this use-case – Frank van Puffelen Feb 29 '16 at 07:59
  • 1
    @FrankvanPuffelen Yes! You have the correct answer. I will leave mine for the NSNull check code example as they go hand-in-hand. – Jay Feb 29 '16 at 16:57
  • @Jay how can I use the logical NOT operator in this if statement? `if snapshot.value is NSNull`. I would like to write `if snapshot.value !== NSNull { //}` , but it does not compile. Thanks – bibscy Feb 11 '18 at 12:37
  • 1
    @bibscy try *if !(snapshot.value is NSNull)* – Jay Feb 11 '18 at 16:07
  • @Jay could you please help me out with this? https://stackoverflow.com/questions/49013963 – bibscy Feb 27 '18 at 17:16