4

I am trying to allow users to start games with and follow other users by searching their username. I need to be able to make sure that a user with that username exists. I was using the following code but although the if is called the else does not get called when it should.

let checkWaitingRef = Firebase(url:"https://test.firebaseio.com/users")
checkWaitingRef.queryOrderedByChild("username").queryEqualToValue("\(username!)")
            .observeEventType(.ChildAdded, withBlock: { snapshot in

    if snapshot.value.valueForKey("username")! as! String == username! {

    } else {

    }

JSON data tree

{
    "097ca4a4-563f-4867ghj0-6209288bd7f02" : {
        "email" : "test1@tes1.com",
        "uid" : "097ca4a4-563f-4867ghj0-6209288bd7f02",
        "username" : "test1",
        "waiting" : "0"
    },
    "55a8f979-ad0d-438u989u69-aa4a-45adb16175e7" : {
        "email" : "test2@test2.com",
        "uid" : "55a8f979-ad0d-438u989u69-aa4a-45adb16175e7",
        "username" : "test2",
        "waiting" : "0"
    }
}
Tom Fox
  • 897
  • 3
  • 14
  • 34

1 Answers1

8

Easy fix:

Don't use .childAdded as the block will not execute when the query doesn't find anything.

Instead use .Value and check for NSNull

    let checkWaitingRef = Firebase(url:"https://test.firebaseio.com/users")
    checkWaitingRef.queryOrderedByChild("username").queryEqualToValue("\(username!)")
                .observeEventType(.Value, withBlock: { snapshot in

            if ( snapshot.value is NSNull ) {
                print("not found)")

            } else {
                print(snapshot.value)
            }
     })
MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
Jay
  • 34,438
  • 18
  • 52
  • 81
  • I tested this approach but the callback function gets called several times. So in the output section I get "not found" and then the snapshot value... not sure how to get only one result – franswa Apr 07 '16 at 21:42
  • You've got something else wrong as .value reads the entire contents of the node so it wouldn't be called multiple times - unless you are modifying the node. You could also do observeSingleEventOfType. – Jay Apr 07 '16 at 22:24
  • Yeah I must have been doing something wrong... I spent my entire evening struggling with it yesterday, ugh, but now it seems to be working like you say – franswa Apr 08 '16 at 06:39
  • Don't forget to also enforce uniqueness on the server, by adding a clause to your security rules: http://stackoverflow.com/questions/37418518/email-verification-using-firebase-3-0-on-android – Frank van Puffelen Jul 01 '16 at 00:05
  • What would be the best security rules for something like this? – Tarvo Mäesepp Nov 15 '16 at 20:16
  • @TarvoMäesepp That's going to depend on your use case - if you can provide and example of what you are doing then we may be able to suggest some rules. Please post that in a separate question. – Jay Nov 16 '16 at 18:19
  • @Jay this is my question pretty much: http://stackoverflow.com/questions/40619433/firebase-username-uniqueness-in-swift/40629722#40629722 – Tarvo Mäesepp Nov 16 '16 at 18:23
  • Wouldn't this take longer the more users you have in your database? – WYS May 10 '17 at 10:39
  • @Wys Not really. Longer is point of perspective; if you have 1,000 users and then next week you have 2,000 users, the difference would be negligible. Ramping that up to 10x that would still not have any significant impact. Firebase is wicked fast and we are only returning a subset of the data. – Jay May 10 '17 at 17:41