下面是我在firebase中的数据库设计
- Sports
- MainCategory (hypothetical category name)
- SubCategory1
- K_tgbBt6Vx0-kkg7e63
- name:football
- K_tgbBt6Vx0-kkg7a99
- name:golf
- Subcategory2
- K_tgbBt6Vx0-kkgad21
- name:Snooker
- K_tgbBt6Vx0-kkg7e00
- name:Table Tennis
- MainCategory2
- SomeOtherSubCategory1
-K_tgbBt6Vx0-kkg7e00我的目标是:将所有的体育项目放在体育节点下,并在一个uicollectionview中显示它们的类别(即MainCategory和子类别),并允许用户选择自己选择的体育项目。
例如:显示
Main Category1
SubCategory1
足球、高尔夫
SubCategory2
乒乓球、斯诺克
Main Category2
SomeOtherSubCategory1
曲棍球、网球
下面的方法只让我在节点中有一个更深的层次
rootref.child("sports").observe(.value, with: { (snapshot) in
let mainCategory = snapshot.key
for child in snapshot.children{
print(child)
}})
rootref.child("sports").observe(.childAdded, with: { (snapshot) in
let mainCategory = snapshot.key
let mySnapshot = snapshot.value! as! NSDictionary
}) { (error) in
print(error.localizedDescription)
}另外,如果有其他方法可以构造我的数据库,请告诉我。
发布于 2017-01-07 18:41:24
`rootref.child("sports").observe(.value, with: { (snapshot) in
//This gets your mainCategory names
let mainCategory = snapshot.key
for child in snapshot.children{
print(child)
}})
rootref.child("sports").observe(.childAdded, with: { (snapshot) in
let mainCategory = snapshot.key
let mySnapshot = snapshot.value! as! NSDictionary
}) { (error) in
print(error.localizedDescription)
}
//Now what you need to do is another snapshot to get the sub categories. It would be something like this:
rootref.child("sports").child("Sub-Category").observe(.value, with: { (snaps) in
//Same steps as you did above
})`
发布于 2017-01-07 19:52:02
很高兴知道像你这样有2.5000名代表的人正在问这样一个问题。我假设您知道MVC,当然,将来您需要一个可重用的服务类来处理Firebase请求,而Model 来处理数据。
接下来,您应该知道在Firebase上观察数据的不同方法。有一个单事件和事件类型。阅读文档:https://firebase.google.com/docs/database/ios/read-and-write
在构造数据库时,Firebase也有一个这样的文档:https://firebase.google.com/docs/database/web/structure-data
不管怎样,我给你做了一个样本,看看:http://www.jsoneditoronline.org/?id=5d9e7067883a538746ace7cdd9e81ebb
我建立了一个新的结构,我相信这是一个更好的结构,你的数据库使用jsoneditoronline网站。尽可能避免这么多嵌套节点。请看我的示例,为了更快、更容易地获取数据,我创建了必要的新节点。例如,如果要查看上面的链接并下载数据库并将其上载到Firebase数据库,您将看到如下结构:

正如您所看到的,这里有一个名为subcategories-sportId,的父节点,它包含不同subcategories的子节点以及每个子类别,,我们有体育项目的ids .。
另一个例子是,如果我们想在subcategory2下获取所有的体育项目,我们将不会使用体育节点中的数据。(见下面的图)但是,我们将检查subcategories-sportid节点中的数据。获取对该节点的引用,并添加特定子类别字符串的子节点,然后获取所有sportsIds。枚举运动员and ,最后获取每项运动的主要数据。

但是如果我们想得到特定运动的所有子类别和主要类别,我们可以使用我们的体育节点中的数据(见上面的图)。
发布于 2017-08-22 09:17:52
let cat3 = db.child("API Questions").child("Category3").child("Name")
cat3.observe(FIRDataEventType.value, with:
{ (snapshot) in
let eventDataloc = snapshot.value as? [String: AnyObject] ?? [:]
// self.Cat3Qarray.removeAllObjects()
for (_, value) in eventDataloc
{
let studsmodel = FirebaseStructureCustVM.updateQuestionData(Questiondata: value as![String:Any])
self.Cat3Qarray.add(studsmodel)
//print(studsmodel)
}
self.tableview1.reloadData()
//print snapshot here
})https://stackoverflow.com/questions/41524616
复制相似问题