【问题标题】:can i upload posts with the same auto id? swift 4 firebase我可以上传具有相同自动 ID 的帖子吗?迅捷4火力基地
【发布时间】:2018-01-03 13:00:17
【问题描述】:

是否可以将帖子上传到 Firebase 到 3 个不同的位置,并且具有相同的 autoId 感觉帖子完全相同。所以我刚刚设法让用户删除他们的帖子,但是当他们上传帖子时,它实际上被上传到了像这样的 3 个不同的位置

// this uploads the "post" to a global page
Database.database().reference().child("posts").childByAutoId().setValue(postObject)

// this uploads the "post" to a specific country page the user have choosen
Database.database().reference().child("AlbaniaPosts").childByAutoId().setValue(postObject)

// this uploads the "post" to the users uid so all their posts will be shown at the profile page
Database.database().reference().child(uid!).childByAutoId().setValue(postObject)

当它们保存在 firebase 中时,它们会保存为不同的 childByAutoId,所以我想知道是否可以将它们全部保存在同一个 childByAutoId ID 中。

因此,如果用户删除了特定帖子,那么我可以搜索他们删除的 AutoId,并将其从所有国家/地区和个人资料页面以及全球页面中删除,但我现在感觉不到我将帖子上传到不同的身份证

所以如果仍然难以理解我的问题,这里是我的数据库的图像

如您所见,帖子完全相同,但它们保存在不同的AutoId's

目标?我的目标是在所有 3 个位置上传帖子时为帖子设置相同的 ID。

非常感谢您的帮助。 感谢您的宝贵时间。

【问题讨论】:

    标签: iphone firebase firebase-realtime-database swift4


    【解决方案1】:

    是的,您可以这样做。问题在于您如何上传信息。获取一把钥匙并重复使用它。像这样:

    let key = ref.child("posts").childByAutoId().key // use this key for all uploads
    
    // this uploads the "post" to a global page
    Database.database().reference().child("posts").child(key).setValue(postObject)
    
    // this uploads the "post" to a specific country page the user have choosen
    Database.database().reference().child("AlbaniaPosts").child(key).setValue(postObject)
    
    // this uploads the "post" to the users uid so all their posts will be shown at the profile page
    Database.database().reference().child(uid!).child(key).setValue(postObject)
    

    现在所有帖子都将使用相同的密钥上传。我在“帖子”上做了childByAutoId(),但你可以在任何地方做。您获得不同密钥的原因是因为它们是由您请求它们的时间决定的。由于您在不同时间请求它们,因此您将获得不同的密钥。

    此外,我认为您可能需要仔细考虑您希望如何访问和修改应用中的数据。例如,修改一篇文章需要在三个不同的位置编辑数据。这个问题可以通过使用云功能来解决。另一种选择是将“密钥”复制到“AlbaniaPosts”之类的内容,然后您可以查询“AlbaniaPosts”下所有帖子的密钥,您可以使用这些密钥从“帖子”加载完整帖子。您仍然需要云功能来复制和删除数据库周围的密钥。如果您有任何问题,请告诉我。

    【讨论】:

    • 但是我的查询确实有点问题,您认为您可以帮忙吗?我正在尝试使帖子显示为示例:A,B,C,但知道它们总是以随机顺序显示,当我使用“timeorder”时,它们显示为 C,B,A 但我想知道如何让它们像 A、B、C 一样显示,所以它就像完美的顺序
    • 是的,发布您的问题,我可以回答。很可能您没有按顺序遍历快照。如果一切都是通过自动 ID 完成的,那么您的“最新”帖子将是最后一个帖子。您还应该查看分页。如果您发布更多问题,请告诉我。
    • 能否请您检查一下这个问题,我想您可以帮助我解决这个问题。谢谢。[stackoverflow.com/questions/48241086/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2019-04-01
    • 1970-01-01
    相关资源
    最近更新 更多