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
  • 887
  • 2
  • 14
  • 32
  • "but although the if is called the else does not get called when I want" What do you mean here? – Khuong Apr 06 '16 at 05:22
  • @khuong291 I mean that the if statement is "activated": `if snapshot.value.valueForKey("username")! as! String == username! {` But even when it should the else statement never does any thing. – Tom Fox Apr 06 '16 at 05:30
  • It correct, because if statement is true, then the else statement never be execute. – Khuong Apr 06 '16 at 05:33
  • @khuong291 no because when the if statement should not be true the else statement does not execute. But I think the problem is that the if statement does not work because of how the firebase query is delivered – Tom Fox Apr 06 '16 at 05:38
  • Can you post more code, what is your username here? And post an image to see your Firebase database? – Khuong Apr 06 '16 at 05:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108372/discussion-between-khuong291-and-tom-fox). – Khuong Apr 06 '16 at 08:25
  • Can you tell what security rules u used? – Tarvo Mäesepp Nov 16 '16 at 15:57

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,903
  • 1
  • 18
  • 35
Jay
  • 32,092
  • 17
  • 52
  • 78
  • 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