-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson_tree.txt
232 lines (197 loc) · 5.25 KB
/
Json_tree.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{users:[
{
"id": "00000000-0000-0000-0000-000000000000",
"createTime": "0001-01-01 00:00:00",
"level": 0,
"nick": "string",
"gender": false,
"birthday": "0001-01-01 00:00:00",
"cellphoneNumber": "string",
"emailAddress": "string",
"name": "string",
"picture": "string""address": "string"
},
{...},
{...},
]
}
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
chatlist:[
{ {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666,
"member":"天涯恋",
"fridlogo":"pic"
"messageid":"青青"
}},
{ ... },
{ ... }]
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// converation ID
"messages": {
"青青": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"明明": { ... },
"亮亮": { ... }
}
}
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
chatlist: [
{
{
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666,
"member": "天涯恋",
"fridlogo": "pic""messageid": "青青"
}
}
,
{ ... },
{ ... }
]
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// converation ID
"messages": {
"青青": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
}
,
"m2": { ... },
"m3": { ... }
}
,
"明明": { ... },
"亮亮": { ... }
}
}
完成对象:
{
users:{userid:
{
"id": "00000000-0000-0000-0000-000000000000",
"createTime": "0001-01-01 00:00:00",
"level": "1",
"nick": "dingding",
"isOnline": false,
"birthday": "0001-01-01 00:00:00",
"cellphoneNumber": "15617771050",
"emailAddress": "[email protected]",
"name": "ake",
"picture": "string",
"address": "科学大道"
}
}
}
{
"chats": {
chatlist: [
{
{
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666,
"member": "天涯恋",
"fridlogo": "pic",
"messageid": "青青"
}
}
]
},
"messages": {
"青青": {[
{
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
}]
}
}
}
https://firebase.google.com/docs/database/android/structure-data
避免嵌套数据
因为 Firebase Realtime Database 允许嵌套数据的深度多达 32 层,您可能不禁认为嵌套应是默认结构。然而,当您提取数据库中某个位置的数据时,也会检索所有子节点。另外,当您向某用户授予数据库中某个节点的读写访问权时,也会将该节点下所有数据的访问权授予该用户。因此,在实践中建议最好将您的数据结构平面化。
为举例说明为什么嵌套数据不正确,现假设一个如下所示的多层嵌套结构:
{
// This is a poorly nested data architecture, because iterating the children
// of the "chats" node to get a list of conversation titles requires
// potentially downloading hundreds of megabytes of messages
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"messages": {
"m1": { "sender": "ghopper", "message": "Relay malfunction found. Cause: moth." },
"m2": { ... },
// a very long list of messages
}
},
"two": { ... }
}
}
若采用这种嵌套设计,循环访问数据就会出现问题。例如,要列出聊天对话标题,就需要将整个 chats 树(包括所有成员和消息)都下载到客户端。
平展数据结构
如果数据被拆分到不同路径(又称反规范化),则可根据需要通过不同调用有效地下载。 请考虑此平面化结构:
{
// 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": { ... }
}
}
现在,每个对话只需下载几个字节即可循环访问房间列表,同时可以快速提取元数据,在 UI 中列出或显示房间。
在消息到达时,可单独提取和显示,从而确保 UI 的及时响应和速度。