Im following Firebase guide to structuring data for a chat app. They suggest the structure as seen below.
{
// Chats contains only meta info about each conver
In the block where you have a list of all the chats a user is in, can you do:
var dictionary: [String: Long]
var lastMessage: String
for chat in listOfChatsUserIsIn
ref.child("chats").child("\(chat)").child("lastMessage").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
lastMessage = snapshot
ref.child("chats").child("\(chat)").child("timestamp").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//add timestamp and last message to dictionary
}
}
I don't know how correct my syntax is. Still learning firebase. But, I think this is basically your second suggestion. Don't know how else you would get the data. This would be O(2n) though which isn't bad.
[[Update 1]]
I was being lazy with my code. I put lastMessage = snapshot to save it so you could add it to the dictionary in the next block.
As for Firebase being asynchronous. I think this would still work as long as you use either the timestamp or message as the key and the other as the value in the dictionary. It may be populated out of order, but you could always sort it by timestamp later. Although, yes, this probably is not best practice.
Jay, I like your solution. Wouldn't you have to also list uid_2: false
?
Unfortunately, it seems like both these database structures grow by n^2 as users -> inf and as chats -> inf.
How do I structure my user data so that I can easily display a list of all of the chats they are part of and for each one of them display the last message and timestamp.
Change the chats structure a tad by adding users who are in the chat node
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
users
uid_1: true
uid_3: true
},
"two": { ... },
Then you can deep query for all chats a particular user is part of - this will return the chats uid_3 is involved in
chatsRef.queryOrderedByChild("users/uid_3").queryEqualToValue(true)
.observeSingleEventOfType(.Value, withBlock: { snapshot in
//.Value can return multiple nodes within the snapshot so iterate over them
for child in snapshot.children {
let lastmsg = child.value["lastMessage"] as! String
let timestamp = child.value["timestamp"] as! String
print(lastmsg)
print(timestamp)
}
})
Note that each firebase user has a discreet user id obtained when the user is created via auth.uid. This should (generally) be used as the key for each user.