gpt4 book ai didi

ios - Firebase:从未知子级检索嵌套数据

转载 作者:行者123 更新时间:2023-11-30 12:27:25 25 4
gpt4 key购买 nike

我有一个像这样的 firebase 结构

"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
}
}
}

我想检索所有具有广播且 c 等于 true 的节点。我怎样才能快速做到这一点?

最佳答案

实现此目的的一种方法是重组数据库:

"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...
}

然后你可以通过调用来获取所有你想要的广播:

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

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

"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 数据库拥抱非规范化!

关于ios - Firebase:从未知子级检索嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com