Firebase中的简单查询

时间:2017-02-02 16:34:33

标签: ios swift firebase firebase-realtime-database

我在Swift中使用Firebase是新手,我看到同一条船上有更多人。我的想法是检索每个球员的得分。 Firebase db是这样的:

enter image description here

哪个是根引用(dbRef = FIRDatabase.database().reference()) 我的代码是这样做的:

dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
        for child in snap.children{
            print(child)
        }
    }

在终端中生成这些消息:

Snap (zfUNewjOCsQ5VCtSLFXSXLSFXtJ3) {
    addedByUser = "julio@gmail.com";
    score = 100;
}
Snap (yfJqO65qCwTTlgzrcw1ovmPgZ6l2) {
    addedByUser = "oswaldo@gmail.com";
    score = 1000;
}

如何获得分数,以便在终端中打印的信息如下:

100

1000

提前致谢

1 个答案:

答案 0 :(得分:1)

您使用childSnapshotForPath来获得分数:

dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
    for child in snap.children{
        print((child as AnyObject).childSnapshot(forPath: "score").value)
    }
}

请参阅https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDataSnapshot#-childsnapshotforpath