【发布时间】:2017-10-13 16:00:29
【问题描述】:
我刚刚开始使用 Firebase 数据库,在高效查询数据方面遇到了一些麻烦。我当前的数据库结构类似于推荐数据结构的 Firebase 文档,我们在其中看到了这个示例:
// An index to track Ada's memberships
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
// Index Ada's groups in her profile
"groups": {
// the value here doesn't matter, just that the key exists
"techpioneers": true,
"womentechmakers": true
}
},
...
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
...
}
}
我遇到的问题是我不确定如何有效地检索特定用户的所有组名。我可以轻松检索用户的所有组 ID:
usersRef.child("alovelace").child("groups").observeSingleEvent(of: .value, with: { (snapshot) in
// code to store group IDs
})
但现在我能想到获取所有组名的唯一方法是通过 ID 进行 for 循环,然后为每个组调用 .observeSingleEvent 以获取名称。但是,如果我有一组非常大的用户组怎么办?理想情况下,我不希望进行如此多的数据库调用,而只需一次调用即可。这种数据结构可以吗?
【问题讨论】:
-
Firebase 将通过单个打开的连接检索项目,因此连接的开销很小。以这种方式(也称为客户端连接)检索合理数量的项目并不像您想象的那么慢,因为请求是通过现有连接进行管道传输的。见stackoverflow.com/questions/35931526/…
标签: ios swift firebase firebase-realtime-database