我刚刚开始使用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来获取名称 . 但是,如果我为用户提供了大量的组,该怎么办?理想情况下,我不想进行如此多的数据库调用,只需在一次调用中完成 . 这种数据结构有可能吗?
2 回答
这就是我建议你做的 . 这将使用用户所属的组树内的所有数据填充groups数组 .
你可以这样做: