【问题标题】:NoSQL data base design for firebase chatFirebase 聊天的 NoSQL 数据库设计
【发布时间】:2017-10-23 18:38:36
【问题描述】:

我在 firebase 的帮助下设计了一个聊天应用程序,但我对同样的数据库设计感到震惊。我已经阅读了firebase structure data for app提供的以下链接

我目前的做法:

"messages" : {
  "-Kkiyu4zSKQAzu3MxpGe": {
    "recId" : 1994,
    "senderId" : 9,
    "senderName" : "Alex",
    "text" : "Hello",
    "time" : "2017-05-22T12:37:41+0530"
  },
  "-Kkiyw1yVTbR_wDFFiuA" : {
    "recId" : 9,
    "senderId" : 1994,
    "senderName" : "Ted",
    "text" : "Hi",
    "time" : "2017-05-22T12:37:49+0530"
  },
  "-KkiywFENy__tCHuuuom" : {
    "recId" : 1994,
    "senderId" : 9,
    "senderName" : "Alex",
    "text" : "What's up?",
    "time" : "2017-05-22T12:37:50+0530"
  },
  "-KkiyxDjsUIXBlM0Cn3R" : {
    "recId" : 9,
    "senderId" : 1994,
    "senderName" : "Ted",
    "text" : "All good",
    "time" : "2017-05-22T12:37:54+0530"
  }

同样,我有用户 JSON 结构,在该结构中,我有 3 种类型的用户,如下所示

1. Branch Manager 2. Delivery boy 3. Admin

"users" : {
  "9" : {
    "empId" : 9,
    "isOnline" : false,
    "name" : "Alex",
    "userType" : "M"
  },
  "1994" : {
    "branchId" : 9,
    "empId" : 1994,
    "isOnline" : false,
    "name" : "Ted",
    "userType" : "D"
  },
  "25" : {
    "empId" : 25,
    "isOnline" : false,
    "name" : "Tim",
    "userType" : "A"
  }
}

和我在应用程序中的听众如下:

sendReference.observe(.childAdded, with: { (snapshot) in
        if let data = snapshot.value {
            let message = RCMessage(object: data)
                message.Id = snapshot.key
            let dbManager = RCPersistencyManager()
                dbManager.add(message: message)
        }
    })

receiveReference.observe(.childAdded, with: { (snapshot) in
        if let data = snapshot.value {
            let message = RCMessage(object: data)
            message.Id = snapshot.key
            let dbManager = RCPersistencyManager()
            dbManager.add(message: message)
            dbManager.update(userwithMessage: message)
        }
    })

我在我的 senderId 和我的 recieverId 上添加了侦听器,我认为这是切割器,因为我将只接收由我发送或定向到我的消息。

sendReference = companyReference.child("messages").queryOrdered(byChild: "senderId").queryEqual(toValue: Int(RCDataManager.get(stringforKey: "RCUserID")!))

receiveReference = companyReference.child("messages").queryOrdered(byChild: "recId").queryEqual(toValue: Int(RCDataManager.get(stringforKey: "RCUserID")!))

但是现在更大的问题来了所有的孩子一次又一次,当孩子的数量超过 200 或者说 300 时,同步数据库需要时间,这会降低我的应用程序的性能我只想获取查询下的新消息,我知道这种设计不是很高兴得到那个真正期待好的答案。

提前致谢

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database chat


    【解决方案1】:

    我收到的数据是在以下条件下创建的每个孩子 查询我确实为应用程序维护了一个本地数据库,但我不想得到 所有的孩子一次又一次地当孩子的数量将 超过 200 或者说 300 同步数据库需要时间 降低我的应用程序的性能我只想获得新消息 在查询下

    您的问题有很多需要解开,但这只是表明您注重细节。试图解开这个问题,您似乎真的只是想将您的查询结果限制为最新的。

    为此,请查看filtering your data

    如果我是你几乎是伪代码,我会这样做:

    1) Initial population of local cache of messages.
    2) On subsequent opens, query your local cache for the 
        newest message and check the `time` at which it was created
    3) In your queries to Firebase, add the queryStartingAtValue and pass it your last synced time and you'll only receive new messages.
    

    在不知道您的 UI 是什么样子的情况下,很难说限制您下载的消息数量 的可行性,因为您需要某种“加载更多”指示器来无缝地做到这一点。该方法与初始加载相同。

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 2017-09-19
      • 1970-01-01
      • 2014-10-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      相关资源
      最近更新 更多