在Firebase中构建聊天应用程序的数据 [英] Structuring data for chat app in Firebase
问题描述
按照Firebase指南,为结构化数据进行聊天应用程序。他们建议如下所示的结构。
{
//聊天只包含每个对话的元信息
//存储在聊天记录的独特ID
chats:{
one:{
title:Historical Tech Pioneers,
lastMessage:ghopper:发现继电器故障原因: ,
timestamp:1459361875666
},
two:{...},
three:{...}
},
//对话成员很容易访问
//并通过聊天对话存储ID
members:{
//我们将讨论索引像下面这样
one:{
ghopper:true,
alovelace:true,
eclarke:true
},
two:{...},
three:{...}
},
//消息与我们想要迭代的数据分开快速
//但仍然很容易分页和查询,并通过聊天组织
// converation ID
messages:{
one:{
m1 :{
name:eclarke,
message:继电器似乎有故障。,
timestamp:1459361875337
},
m2:{...},
m3:{...}
},
two:{...},
three: {...}
}
}
如何组织我的用户数据,以便我可以轻松地显示所有聊天的列表,并为每个聊天显示最后的消息和时间戳。如果我做了以下结构:
$ $ p $ users:{
ghopper:{
名称:Gary Hopper,
chats:{
one:true,
two:true
}
},
alovelace{...}
},
(swift):
ref.child(users ).child(ghopper)。child(chats)。observeSingleEventOfType(.Value,withBlock:{(snapshot)in
// do something with data
}
然而,我不会在这个快照中使用lastMessage和timestamp,我需要做些什么来访问这些数据呢? p>
- 为每个用户复制所有这些数据,即添加用户/ ghopper /聊天/一个/ {lastMessage:ghopper:发现中继故障。原因:飞蛾。,timestamp:1459361875666}
- 为每个查询chats / specificGroupId聊天,该用户是(添加多个列表)的一部分?
- 其他一些方法?
解决方案
如何构建我的用户数据,以便我可以轻松地显示
列表中的所有聊天内容,并为每个聊天显示
最后一条消息和时间戳。
通过添加聊天节点中的用户来改变聊天结构。
chats:{
one:{
title:Historical Tech Pioneers,
lastMessage:ghopper :
timestamp:1459361875666
users
uid_1:true
uid_3:true
},
两个:{...},
然后您可以深入查询特定用户的所有聊天记录部分 - 这将返回聊天uid_3参与
$ b $ pre $chatsRef.queryOrderedByChild(users / uid_3)。queryEqualToValue(true )
.observeSingle EventOfType(.Value,withBlock:{snapshot in
//.Value可以返回快照中的多个节点,以便迭代它们
在snapshot.children中的子节点{
let lastmsg = child.value [lastMessage]为!字符串
让timestamp = child.value [timestamp] as!字符串
print(lastmsg)
print(timestamp)
}
})
请注意,当用户通过auth.uid创建时,每个firebase用户都有一个谨慎的用户标识。这应该(通常)作为每个用户的关键。
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 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": { ... } } }
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. If I do the following structure:
"users": { "ghopper": { "name": "Gary Hopper", "chats": { "one: true", "two": true } }, "alovelace" { ... } },
I can easily get a list of each chat group for a specific user, for example ghopper, by doing (in swift):
ref.child("users").child("ghopper").child("chats").observeSingleEventOfType(.Value, withBlock: { (snapshot) in //do something with data }
However I won't have the lastMessage and timestamp in this snapshot. What do I need to do to access this data?
- Duplicate all this data for each user? i.e adding users/ghopper/chats/one/ {"lastMessage": "ghopper: Relay malfunction found. Cause: moth.", "timestamp" : 1459361875666}
- Make a query for "chats/specificGroupId" for each chat that the user is part of (adding multiple listners)?
- Some other way?
解决方案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.
这篇关于在Firebase中构建聊天应用程序的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!