對於聊天應用程序,我遵循Firebase指南structuring data。他們建議如下所示的結構。爲Firebase中的聊天應用程序構造數據
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// converation ID
"messages": {
"one": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"two": { ... },
"three": { ... }
}
}
如何構建我的用戶數據,這樣我可以很容易地顯示所有他們的部分聊天記錄的列表,併爲他們的每一個顯示的最後一條消息和時間戳。如果我做了以下結構:
"users": {
"ghopper": {
"name": "Gary Hopper",
"chats": {
"one: true",
"two": true
}
},
"alovelace" { ... }
},
我可以輕鬆地獲得每個聊天組列表針對特定用戶,例如ghopper,這樣做(在SWIFT):
ref.child("users").child("ghopper").child("chats").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//do something with data
}
但是我贏了此快照中沒有lastMessage和時間戳記。我需要做什麼來訪問這些數據?
- 爲每個用戶複製所有這些數據?即添加用戶/ ghopper /聊天/ one/{「lastMessage」:「ghopper:發現中繼故障原因:飛蛾。」,「時間戳」:1459361875666}
- 爲每個聊天記錄查詢「chats/specificGroupId」用戶是(添加多個listners)的一部分?
- 還有其他的一些方法嗎?
因爲您沒有使用該變量,lastMesssage = snapshot的功能是什麼?另外,在塊中使用self.variable名稱是最好的做法(閉包)。另外,第一個ref.child中的ref.child是重複的,所以它會讀取相同的數據。最大的問題是Firebase是異步的,並且緊密的for循環將比觀察塊內的數據可以返回的方式運行快得多,所以我認爲我們遇到了一些數據完整性問題 - 可能應該讓Firebase做到這一點'東西'異步。 – Jay
要回答您的答案....不,不需要uid_2:false,因爲它不存在,它可能默認爲false(取決於它的編碼)。不,數據庫不會呈指數增長。如果你有10個用戶和1個聊天,那麼所有10個用戶都在聊天,所以唯一增長的將是聊天中的用戶節點(在我的答案中),如果你有1000個用戶和100個聊天,每個聊天與兩個用戶,那仍然只有100個聊天,每個聊天都有一個有兩個用戶的用戶節點。即使有1000個用戶和1000個聊天記錄,這也不算大量的數據。 – Jay