Firebase:从未知的孩子中检索嵌套的数据 [英] Firebase: Retrieve nested data from unknown children

查看:130
本文介绍了Firebase:从未知的孩子中检索嵌套的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的firebase结构

I have a firebase structure like this

"broadcast": {
    "ghjghjFc1S3KO0y8yJwORdfgret": {   //user ID
         "ryrtybgzMiI858YyGua": {  //broadcast ID
              "a": "xxx",   // broadcast Detail
              "b": "yyy",
              "c": false
         }
         "cbvbcvbMvAnSDqTb15vU": {  //broadcast ID
              "a": "xxx",   // broadcast Detail
              "b": "yyy",
              "c": true
         }
    }
    "3uqWZJRFc1S3KO0y8yJwORTMtWC2": {   //user ID
         "jkhjkbgzMiI858YyGua": {  //broadcast ID
              "a": "xxx",   // broadcast Detail
              "b": "yyy",
              "c": false
         }
         "qwwerqweMvAnSDqTb15vU": {  //broadcast ID
              "a": "xxx",   // broadcast Detail
              "b": "yyy",
              "c": true
         }
    }
}

我wa nt来检索所有具有广播的节点,其中c等于真。如何在swift中执行此操作?

I want to retrieve all nodes having broadcasts where c is equal to true. How can I do this in swift?

推荐答案

b
$ b

One way you could do this is by restructuring your database:

"userBroadcasts": {
    "ghjghjFc1S3KO0y8yJwORdfgret": { // user ID
        "ryrtybgzMiI858YyGua": true, // broadcastID
        "jkhjkbgzMiI858YyGua": true, // another broadcastID
        // etc..
    },
    // more users down here...
}

"broadcast": {
    "ryrtybgzMiI858YyGua": { // broadcast ID
        "a": "xxx",   // broadcast Detail
        "b": "yyy",
        "c": false
    },
    "jkhjkbgzMiI858YyGua": {
        "a": "xxx",
        "b": "yyy",
        "c": true
    },
    // more broadcasts down here...
}

然后你可以通过调用获得所有的广播:

Then you can get all the broadcasts you want by calling:

FIRDatabase.database().reference().child("broadcast").queryOrdered(byChild: "c").queryEqual(toValue: true)

如果您还想使用userID,请在创建时将其存储在广播中例如:

If you want the userID as well, store this inside the broadcast when you create it, for example:

"broadcast": {
    "ryrtybgzMiI858YyGua": { // broadcast ID
        "a": "xxx",   // broadcast Detail
        "b": "yyy",
        "c": false,
        "userID":"ghjghjFc1S3KO0y8yJwORdfgret"
    },
    "jkhjkbgzMiI858YyGua": {
        "a": "xxx",
        "b": "yyy",
        "c": true,
        "userID":"ghjghjFc1S3KO0y8yJwORdfgret"
    },
    // more broadcasts down here...
}

拥有Firebase数据库的反规范化功能!

Embrace denormalisation with the Firebase Database!

这篇关于Firebase:从未知的孩子中检索嵌套的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆