1
T
R
E I
•COMIC•
D
3
5
O

Firebase Realtime Database 允许直接从客户端代码中直接安全访问数据库,因此您能够构建丰富的协作式应用。 数据保留在本地,即使处于离线状态,实时事件仍继续触发,给最终用户提供一种响应式体验。当设备重新取得连接时, Realtime Database 会将本地数据变化与客户端离线期间发生的远程更新同步,自动合并任何不一致数据。

-
集成 Firebase Realtime Database SDK。
通过 Gradle、CococaPods 或脚本包含来快速包含客户端。
-
创建 Realtime Database 引用。
为设置数据或订阅数据变化,请引用您的 JSON 数据,如"users/user:1234/phone_number"。
-
设置数据和侦听变化。
使用此引用写入数据或订阅变化。
-
启用离线留存。
允许将数据写入到设备的本地磁盘,以便离线时使用。
-
保障数据安全。
使用 Firebase Realtime Database 安全规则保障您的数据安全。

-
Install the Firebase SDK.
见《将Firebase添加到项目》
不会做超链接,有知道的告知下,谢谢
-
在Firebase控制台添加一个自己的项目。
-
添加依赖
-
compile 'com.google.firebase:firebase-database:10.2.6'
4. 配置Firebase Database规则
默认情况下,您的数据库规则会授予完全读取和写入权限,但仅授予给已通过身份验证的用户。 以下为默认规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
如果您只是刚开始使用并希望在配置安全规则之前先试用数据库,则可使用以下规则授予完全的公共数据库访问权限:
{
"rules": {
".read": true,
".write": true
}
}
在发布您的应用之前必须正确配置这些规则,以确保您的用户只能访问他们应该能够访问的数据。
如何使用安全规则保障数据安全

# Add this global rule
-keepattributes Signature
# This rule will properly ProGuard all the model classes in
# the package com.yourcompany.models. Modify to fit the structure
# of your app.
-keepclassmembers class com.yourcompany.models.** {
*;
}

构建一个结构合理的数据库需要预先进行大量计划。最重要的是,您需要对如何保存数据及之后如何检索数据做好计划,尽可能简化保存和检索的进程。
所有 Firebase Realtime Database 数据都被存储为 JSON 对象。您可将该数据库视为云托管 JSON 树。 该数据库与 SQL 数据库不同,没有任何表格或记录。 当您将数据添加至 JSON 树时,它变为现有 JSON 结构中的一个节点。
例如,假设聊天应用允许用户存储基本个人资料和联系人列表。 通常用户个人资料位于一个诸如 /users/$uid
之类的路径中。 用户 alovelace
的数据库项看起来可能如下所示:
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
"contacts": { "ghopper": true },
},
"ghopper": { ... },
"eclarke": { ... }
}
}
数据结构最佳做法
-
避免嵌套数据
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 的及时响应和速度。
-
创建可扩展的数据
假设用户与群组之间存在双向关系。 用户可属于一个群组,且群组包含一个用户列表。 当需要决定用户属于哪些群组时,情况就会比较复杂。
我们需要的是一种完善方法,不仅列出用户所属群组,而且只提取这些群组的数据。 对群组的"索引"在此可能有很大的帮助:
// An index to track Ada's memberships
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
// Index Ada's groups in her profile
"groups": {
// the value here doesn't matter, just that the key exists
"techpioneers": true,
"womentechmakers": true
}
},
...
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
...
}
}
这种索引是通过存储 Ada 记录及该群组下的关系来重复某些数据的。 现在,alovelace
在一个群组下进行索引,而 techpioneers
则列在 Ada 的个人资料中。 所以要从该群组删除 Ada,则必须在两个地方更新。
对于双向关系而言,这是必要的冗余。这样,您就可以快速、高效地提取 Ada 的成员身份,而且即使用户或群组列表有数百万条记录,或 Realtime Database 安全规则会阻止访问某些记录,也不受影响。
这种方法通过将 ID 列为密钥并将值设为 true 来颠倒数据,使密钥检查变得像读取 /users/$uid/groups/$group_id
和检查是否 null
一样简单。与查询或扫描数据相比,索引的速度更快,效率更高。

有四种方法可以将数据写入 Firebase Realtime Database:
setValue():
常见用法:将数据写入或替换到定义的路径,如 users/<user-id>/<username>
。
push():
常见用法: 添加到数据列表。每次调用 push()
时,Firebase 均会生成唯一 ID,如