Firebase数据的结构为JSON。根据最佳实践,我们应该创建非规范化的数据形式。我们应该在不同节点中推送相同的数据。根据他们的文档,可以在不同的分支中复制数据。
我应该如何在Firebase中构建此数据?
我正在编写一个PARSE中的博客应用程序,并希望迁移到Firebase。
我的每个Blog都有不同的hashTag。这些HashTag是可点击的。因此,当我们点击特定的hashTag时,它将重定向到具有相同hashTag的公共博客的页面。
我们如何在Firebase中概念化上述哈希标记行为。 如何构建数据,以便我可以查询所有博客的特定主题标签?
喜欢select * from [blogs] where tag = '#hashtag'
答案 0 :(得分:1)
试试这个
blogs
blog_01
hashtag:"#hashtag"
data: "some data"
blog_02
hashtag: "#anotherHashtag"
data: "more data"
blog_03
hashtag: "#superHashtag"
data: "another data"
和代码
ref = rootRef.childByAppendingPath("blogs")
ref.queryOrderedByChild("hashtag").queryEqualToValue("#anotherHashtag")
.observeEventType(.Value, withBlock: { snapshot in
//.Value can return multiple nodes within the snapshot so iterate over them
for child in snapshot.children {
let hash = child.value.objectForKey("data") as! String
print(hash) //prints 'more data'
}
})
编辑:这是OS X Swift代码,但您可以获得跨平台适用的一般概念。
答案 1 :(得分:1)
我认为Firebase希望你做类似的事情:
{
"blogs": {
"blog1": {
"name": "blogpost1",
"text": "blogpost1 text"
"tags": {
"tag1": true,
"tag2": true,
"tag3": true
}
},
"blog2": {
"name": "blogpost2",
"text": "blogpost2 text"
"tags": {
"tag1": true
}
},
"blog3": {
"name": "blogpost3",
"text": "blogpost3 text"
"tags": {
"tag1": true
}
}
}
{
"tags": {
"tag1": {
"blog1": true,
"blog2":true,
"blog3":true,
},
"tag2": {
"blog1":true
},
"tag3": {
"blog1":true
}
}
}
我希望这会有所帮助。基本上,您将使用包含相应博客帖子的密钥的每个标记来查询您的标签json。一个好的下一步可能是,与将标签中的值设为true相反,放置一个日期,以便您可以按顺序在搜索结果中订购帖子。
请提出您想出的解决方案。