ios - Firebase 数据库架构 - 像 Snapchat

标签 ios swift firebase google-cloud-firestore

我想用 Firebase 创建一个应用。

我有这个应用程序的两个主要目标:

  1. 获取好友关系
  2. 发送图片给 friend

在我的 Firestore 数据库中,我有“用户”集合,其中用户 ID 作为文档,字段中有 ID 和电子邮件。 (用户> ID > ID,电子邮件)。当用户注册时,我在数据库中发送信息。

        db.collection("users").document(currentUser.uid).setData([
            "id": currentUser.uid,
            "email": currentUser.email ?? ""
        ]) { err in
            if let err = err {
                print("Error writing document: \(err)")
            } else {
                print("Document successfully written!")
            }
        }

我想如果我想处理 friend 关系,我必须在文档用户中添加集合(用户> id > id,电子邮件(字段)... friendsRelationship(集合)> id(添加为字段的用户)) .

并将此添加到其他用户文档 ID 用户 (targetedUser) --> 双向。

当某个用户删除另一个用户时,我们必须从 currentUser 和 targetedUser 中删除 friendsRelationship 集合中的 id。

对于 Firebase 中的好友关系,是否还有其他解决方案/更好的数据库架构?

当好友关系完成后,我们必须将图片发送给targetedUser。

我宁愿做 users > id > images (collection) > url, date (fields)... 或 images > id (generated) > fromUser (id), toUserId (collection) > id ?

如何查询当前用户收到的所有图片? 像 db.collection("users").document(currentUser.uid) ...

这是我的 Cloud Firestore 数据库的屏幕截图: https://imgur.com/4bGJOoP

当 firstUser 向其他用户发送 snap 时,我不知道如何用它来管理 friend 关系,并像 snapshat 一样发布。

感谢您的建议!

最佳答案

I think if I want to take care of friend relationship, I have to add collection inside document user (users > id > id, email (fields) ... friendsRelationship (collection) > id (users added as field)).

我觉得你存储用户数据的方式不错。

Is there any other solution / better database architecture to use for friend relationship in Firebase ?

关于 friend 关系,还有另一种可能的解决方案,它不需要创建子集合,您可以简单地将每个 friend ID 添加到用户文档中的数组中。新架构可能如下所示:

Firestore-root
   |
   --- users
        |
        --- uid
             |
             --- email: "name@email.com"
             |
             --- id: "userId"
             |
             --- friendIds: ["friendIdOne", "friendIdTwo", "friendIdThree"]

如果文档大小未达到限制,这将完全正常工作。因此,对于可以放入文档的数据量存在一些限制。根据有关 usage and limits 的官方文档:

Maximum size for a document: 1 MiB (1,048,576 bytes)

如您所见,单个文档中的数据总量不得超过 1 MiB。当我们谈论存储文本时,您可以存储很多内容。

如果您认为文档的大小可能会超过该限制,那么将 friend 作为文档存储在单独集合中的解决方案将解决您的问题。

And to add this in the other user document id user (targetedUser) --> bidirectional.

要建立双向关系,在上面的解决方案中,您应该简单地复制 friendId在用户的 friendIds数组和 userId在 friend 的friendIds数组。

When some one user delete the other user, we have to delete the id in friendsRelationship collection from currentUser and targetedUser.

在上面的解决方案中,您应该从 friendIds 中删除 userId数组和 friendId来自用户的 friendIds数组。

I'd rather do users > id > images (collection) > url, date (fields)... or images > id (generated) > fromUser (id), toUserId (collection) > id ?

在这种情况下,您还可以使用数组来存储图像引用。同样,如果您认为文档的大小可能会超过该限制,那么将图像作为文档存储在单独的集合中的解决方案是可以继续使用的解决方案。

How to query all images that current User has received ? Like db.collection("users").document(currentUser.uid) ...

是的,通过获取对用户文档的引用:

db.collection("users").document(currentUser.uid)

然后阅读 images数组。

关于ios - Firebase 数据库架构 - 像 Snapchat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440472/

相关文章:

node.js - 如何使用私有(private) NPM 包和云功能?

ios - 从数组中查找地址并将其发送到其他 View

iOS 在 Modal View Controller 旋转时旋转 View Controller

sqlite - 在 Swift 中从 SQL 查询中排除值

ios - 导航按钮不显示

firebase - 未定义名称 'firebase' flutter

ios - iOS 11 deferredLocationUpdatesAvailable始终返回NO,并且Error Domain = kCLErrorDomain Code = 11。如何使deferredlocationupdate工作?

ios - Swift Firebase Like Post in Table View

swift - 使用 MQTT 实现离线聊天

android - Firebase 离线功能和 addListenerForSingleValueEvent