Ios Firebase数据检索,路径内部路径
我想知道在当前路径的完成块内是否可以访问其他路径Ios Firebase数据检索,路径内部路径,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我想知道在当前路径的完成块内是否可以访问其他路径 我使用它的方式如下。。。我有一个社交媒体应用程序,带有“Posts”路径。很明显,这是我获取所有帖子信息的地方。我想为每个帖子创建“评论”。这就是我希望有一条“评论”之路的地方。有人对实现这一目标有什么建议 下面是它应该如何工作的示例代码。在我的项目中使用相同的方法。当然,路径是假的,所以用实际路径填充它 // Read all posts from Firebase ref.child("posts").observeEventType(.Va
我使用它的方式如下。。。我有一个社交媒体应用程序,带有“Posts”路径。很明显,这是我获取所有帖子信息的地方。我想为每个帖子创建“评论”。这就是我希望有一条“评论”之路的地方。有人对实现这一目标有什么建议 下面是它应该如何工作的示例代码。在我的项目中使用相同的方法。当然,路径是假的,所以用实际路径填充它
// Read all posts from Firebase
ref.child("posts").observeEventType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
// Loop through all posts
for snap in snapshots {
// Read comments for each post. snap.key in code below represents post key. Don't know if you have same structure so fill your data here.
ref.child("comment").child(snap.key).observeEventType(.Value, withBlock: { snapshot in
if let postDictionary = snapshot.value as? Dictionary<String, AnyObject> {
// Here you have comments for current post
}
})
}
}
})
//从Firebase读取所有帖子
ref.child(“posts”).observeEventType(.Value,with block:{snapshot in
如果让快照=snapshot.children.allObjects为?[FIRDataSnapshot]{
//循环浏览所有帖子
用于管理单元快照{
//阅读每篇文章的评论。下面的snap.key代码代表文章的关键字。不知道你们是否有相同的结构,所以在这里填写你们的数据。
ref.child(“comment”).child(snap.key).observeEventType(.Value,with block:{snapshot in
如果让postDictionary=snapshot.value作为字典{
//这里有你对当前帖子的评论
}
})
}
}
})
以下是它应该如何工作的示例代码。在我的项目中使用相同的方法。当然,路径是假的,所以用实际路径填充它
// Read all posts from Firebase
ref.child("posts").observeEventType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
// Loop through all posts
for snap in snapshots {
// Read comments for each post. snap.key in code below represents post key. Don't know if you have same structure so fill your data here.
ref.child("comment").child(snap.key).observeEventType(.Value, withBlock: { snapshot in
if let postDictionary = snapshot.value as? Dictionary<String, AnyObject> {
// Here you have comments for current post
}
})
}
}
})
//从Firebase读取所有帖子
ref.child(“posts”).observeEventType(.Value,with block:{snapshot in
如果让快照=snapshot.children.allObjects为?[FIRDataSnapshot]{
//循环浏览所有帖子
用于管理单元快照{
//阅读每篇文章的评论。下面的snap.key代码代表文章的关键字。不知道你们是否有相同的结构,所以在这里填写你们的数据。
ref.child(“comment”).child(snap.key).observeEventType(.Value,with block:{snapshot in
如果让postDictionary=snapshot.value作为字典{
//这里有你对当前帖子的评论
}
})
}
}
})
我强烈建议先阅读我关于类似问题的经典答案:
通常不建议在Firebase数据库中嵌套数据。原因有很多,但有几个:
- 您只能检索完整的节点。因此,如果您在每个帖子下嵌套评论,这意味着您将在收到帖子时自动获得所有评论
- 您通常希望对每种类型(帖子和评论)使用不同的访问规则。当您嵌套它们时,这更难管理,因为权限会逐渐下降
帖子
和评论
posts
$postid
author: "uidOfCoderCody"
title: "Firebase Data Retrieval, Path Inside Path"
body: "I would like to know if it possible to..."
comments
$postid
$commentid
author: "uidOfZassX"
comment: "Here is sample code of how it should work."
由于注释存储在帖子本身的$postid
下,因此您可以轻松地查找帖子的注释
根据应用程序涵盖的用例,您需要调整或(更有可能)扩展此数据模型,以高效地允许您的用例。要了解更多信息,我还建议您阅读这篇文章。我强烈建议您先阅读我关于类似问题的经典答案: 通常不建议在Firebase数据库中嵌套数据。原因有很多,但有几个:
- 您只能检索完整的节点。因此,如果您在每个帖子下嵌套评论,这意味着您将在收到帖子时自动获得所有评论
- 您通常希望对每种类型(帖子和评论)使用不同的访问规则。当您嵌套它们时,这更难管理,因为权限会逐渐下降
帖子
和评论
posts
$postid
author: "uidOfCoderCody"
title: "Firebase Data Retrieval, Path Inside Path"
body: "I would like to know if it possible to..."
comments
$postid
$commentid
author: "uidOfZassX"
comment: "Here is sample code of how it should work."
由于注释存储在帖子本身的$postid
下,因此您可以轻松地查找帖子的注释
根据应用程序涵盖的用例,您需要调整或(更有可能)扩展此数据模型,以高效地允许您的用例。要了解更多信息,我还建议您在上阅读这篇文章。一旦我能够接受,我会立即阅读。谢谢你的帮助!一旦我能接受它,我会的。谢谢你的帮助!我更喜欢你的策略。我猜我可以使用点符号访问帖子的自动ID?我不知道你的意思。你绝对可以获得帖子的自动ID/密钥。调用
let newPostRef=ref.childByAutoId()
后,您可以通过newPostRef.key
@CoderCody获得它。我下面的答案是,如果结构与上述结构类似,如何从Firebase读取。我更喜欢您的策略。我猜我可以使用点符号访问帖子的自动ID?我不知道你的意思。你绝对可以获得帖子的自动ID/密钥。调用let newPostRef=ref.childByAutoId()
后,您可以通过newPostRef.key
@CoderCody获得它。我下面的答案是,如果结构与上面的类似,如何从Firebase读取。