T Neate
T Neate

Reputation: 413

Firebase on Swift, how to get notified if a query is empty?

Hello I am using firebase in swift and I am trying to search through my users by user name using a query like so:

InfoCenter.ref.child("users").queryOrderedByChild("username").queryEqualToValue(firstTextField.text)
            .observeEventType(.ChildAdded, withBlock: { snapshot in

This works great but I want to be notified if the query is empty aka the there is no user that has that user name. Can someone please show me how to edit how I format my search to allow for that? Thanks!

Edit:

My tree looks like this:

users:{

asdfasdfkl:{

username: "bob",

highscore: "1000"

}

}

upon changing event type to .Value, when using snapshot.value I no longer get the adjkadfjla ID. I now get the entire try below it

Upvotes: 1

Views: 883

Answers (2)

Senator
Senator

Reputation: 83

for Swift 5.0, it's been updated to if (querySnapshot?.isEmpty == true) {}

Upvotes: 0

SwiftER
SwiftER

Reputation: 1255

you can check if the snapshot exist, this will return a Bool

snapshot.exists()

Upvotes: 1

Related Questions