Firebase包含值的结果数 - iOS Swift

问题描述 投票:1回答:1

我有一个相当简单的场景,我试图获得具有特定喜欢颜色的用户数量。例如,我想了解一下喜欢“Blue”颜色的用户数量。

enter image description here

以下代码将获取每个用户拥有的子节点数,在本例中为4(favoriteColor,displayName,email和provider)。我想改为拥有特别喜欢的颜色的用户数量。

let ref = Firebase(url: "https://project.firebaseio.com/users")
ref.queryOrderedByChild("favoriteColor").queryEqualToValue("Blue").observeEventType(.ChildAdded, withBlock: { snapshot in
            print(snapshot.childrenCount)

    })

我试图通过UILabel实时计算具有特定喜欢颜色的用户数量,因此每次结果数量发生变化时我都会更新标签。

目前有办法做到这一点吗?

ios swift firebase
1个回答
1
投票

我通过将.ChildAdded改为.Value来解决这个问题。

我也将observeEventType改为observeSingleEventOfType

let ref = Firebase(url: "https://project.firebaseio.com/users")
    ref.queryOrderedByChild("favoriteColor").queryEqualToValue("Blue").observeSingleEventOfType(.Value, withBlock: { snapshot in
    print(snapshot.childrenCount)

})

希望这有助于将来的某些人!

© www.soinside.com 2019 - 2024. All rights reserved.