Reece Kenney Reece Kenney - 6 months ago 23
iOS Question

Firebase number of results that contain a value - iOS Swift

I have a fairly simple scenario whereby I am trying to get the number of users with a particular favorite color. For example, I would like to retreive the number of users that have a favorite color of 'Blue'.

enter image description here

The following code will get me the number of children nodes each user has which in this case is 4 (favoriteColor, displayName, email and provider). I would instead like to get the number of users that have a particular favorite color.

let ref = Firebase(url: "")
ref.queryOrderedByChild("favoriteColor").queryEqualToValue("Blue").observeEventType(.ChildAdded, withBlock: { snapshot in


I am trying to keep a live count of the number of users with a particular favorite color via UILabel so I will update the label each time there is a change to the number of results.

Is there currently a way to do this?


I solved this by changing .ChildAdded to .Value.

I also changed observeEventType to observeSingleEventOfType

let ref = Firebase(url: "")
    ref.queryOrderedByChild("favoriteColor").queryEqualToValue("Blue").observeSingleEventOfType(.Value, withBlock: { snapshot in


Hope this helps someone in future!