使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Firebase Realtime Database
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
利用我们的 NoSQL 云端数据库存储和同步数据。数据会实时同步到所有客户端,而且即便您的应用处于离线状态,数据也仍然可供使用。
Firebase Realtime Database 是一种托管在云端的数据库。数据以 JSON 格式存储并实时同步到所连接的每个客户端。当您使用我们的 Apple 平台、Android 和 JavaScript SDK 构建跨平台应用时,所有的客户端都共享一个 Realtime Database 实例,并自动接收包含最新数据的更新。
或者,如果您的应用需要更丰富的数据模型、可查询性、可伸缩性和更高的可用性,不妨考虑尝试使用 Cloud Firestore。
准备好开始了吗?选择您的平台:
iOS+
Android
Web
Flutter
Unity
C++
Admin
REST API
主要功能
实时 |
Firebase Realtime Database 使用的不是常见的 HTTP 请求,而是数据同步机制。每当数据发生变化时,任何连接的设备都会以毫秒级的速度收到该更新。它可提供沉浸式的协作体验,无需考虑网络因素。 |
离线 |
Firebase 应用即使在离线状态仍会保持响应,因为 Firebase Realtime Database SDK 会将您的数据始终保留在磁盘上。一旦重新建立连接,客户端设备会立即接收到之前错过的任何更改,以便与当前服务器状态保持同步。 |
可从客户端设备访问 |
移动设备或网络浏览器可以直接访问 Firebase Realtime Database,因此不需要应用服务器。安全和数据验证均通过 Firebase Realtime Database 安全规则提供,这些基于表达式的规则会在读取或写入数据时执行。 |
扩容到多个数据库 |
选择按照 Blaze 定价方案使用 Firebase Realtime Database 时,可以通过将数据拆分至同一 Firebase 项目中的多个数据库实例,来支持大规模的应用数据需求。在您的项目中使用 Firebase Authentication 可以简化身份验证,并可跨多个数据库实例验证用户。使用针对每个数据库实例的自定义 Firebase Realtime Database Security Rules 规则,您可以控制对各个数据库中的数据访问。 |
工作原理
Firebase Realtime Database 允许从客户端代码中直接安全地访问数据库,得益于此,您能够构建功能丰富的协作式应用。数据会永久性地保留在本地,即使处于离线状态,实时事件仍会继续触发,给最终用户提供良好的即时性体验。当设备重新连接到网络时,Realtime Database 会将本地数据变化与客户端离线期间发生的远程更新同步,自动合并任何不一致的数据。
Realtime Database 提供灵活的基于表达式的规则语言(称为 Firebase Realtime Database 安全规则),可以定义您的数据结构以及何时可以读取或写入数据。当与 Firebase Authentication 集成时,开发者可以定义什么人可以访问什么数据以及如何访问数据。
Realtime Database 是一种 NoSQL 数据库,因此具有不同于关系型数据库的优化方向和功能特点。Realtime Database API 的设计只支持可以快速执行的操作。这使您能够构建实时性更强的体验,即使服务数百万的用户,响应速度也丝毫不受影响。因此,请务必认真考虑用户需要如何访问您的数据,以便相应地设计数据结构。
实现流程
|
集成 Firebase Realtime Database SDK |
使用 Gradle、CocoaPods 或脚本添加方式快速添加客户端。 |
|
创建 Realtime Database 引用 |
引用您的 JSON 数据(如“users/user:1234/phone_number”)来设置数据或订阅数据变化。
|
|
设置数据并监听变化 |
使用这些引用写入数据或订阅变化。 |
|
启用离线持久留存 |
允许将数据写入设备本地磁盘,以便供离线时使用。 |
|
保护您的数据 |
使用 Firebase Realtime Database 安全规则来保护您的数据。 |
存储其他类型的数据
后续步骤:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-06。
[null,null,["最后更新时间 (UTC):2025-08-06。"],[],[],null,["Firebase Realtime Database \nplat_ios plat_android plat_web plat_flutter plat_cpp plat_unity \nStore and sync data with our NoSQL cloud database. Data\nis synced across all clients in realtime, and remains available when your\napp goes offline. \n\nRealtime Database \n\nCloud Firestore Preferred \nThe Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON\nand synchronized in realtime to every connected client. When you build\ncross-platform apps with our Apple platforms, Android, and JavaScript SDKs, all of your\nclients share one Realtime Database instance and automatically receive updates with\nthe newest data.\n\nAlternatively, consider trying [Cloud Firestore](/docs/firestore) for modern\napplications requiring richer data models, queryability, scalability and higher\navailability.\n\n\u003cbr /\u003e\n\nReady to get started? Choose your platform:\n\n[iOS+](/docs/database/ios/start)\n[Android](/docs/database/android/start)\n[Web](/docs/database/web/start)\n[Flutter](/docs/database/flutter/start)\n[Unity](/docs/database/unity/start)\n[C++](/docs/database/cpp/start)\n[Admin](/docs/database/admin/start)\n[REST API](/docs/database/rest/start) \n\nKey capabilities\n\n|---------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Realtime | Instead of typical HTTP requests, the Firebase Realtime Database uses data synchronization---every time data changes, any connected device receives that update within milliseconds. Provide collaborative and immersive experiences without thinking about networking code. |\n| Offline | Firebase apps remain responsive even when offline because the Firebase Realtime Database SDK persists your data to disk. Once connectivity is reestablished, the client device receives any changes it missed, synchronizing it with the current server state. |\n| Accessible from Client Devices | The Firebase Realtime Database can be accessed directly from a mobile device or web browser; there's no need for an application server. Security and data validation are available through the Firebase Realtime Database Security Rules, expression-based rules that are executed when data is read or written. |\n| Scale across multiple databases | With Firebase Realtime Database on the Blaze pricing plan, you can support your app's data needs at scale by splitting your data across multiple database instances in the same Firebase project. Streamline authentication with Firebase Authentication on your project and authenticate users across your database instances. Control access to the data in each database with custom Firebase Realtime Database Security Rules for each database instance. |\n\nHow does it work?\n\nThe Firebase Realtime Database lets you build rich, collaborative applications\nby allowing secure access to the database directly from client-side code. Data\nis persisted locally, and even while offline, realtime events continue to fire,\ngiving the end user a responsive experience. When the device regains connection,\nthe Realtime Database synchronizes the local data changes with the remote updates\nthat occurred while the client was offline, merging any conflicts automatically.\n\nThe Realtime Database provides a flexible, expression-based rules language,\ncalled Firebase Realtime Database Security Rules, to define how your data should be\nstructured and when data can be read from or written to. When integrated with\nFirebase Authentication, developers can define who has access to what data, and how\nthey can access it.\n\nThe Realtime Database is a NoSQL database and as such has different optimizations\nand capabilities compared to a relational database. The Realtime Database API is\ndesigned to only allow operations that can be executed quickly. This lets you\nbuild a great realtime experience that can serve millions of users without\ncompromising on responsiveness. Because of this, it is important to think about\nhow users need to access your data and then\n[structure it accordingly](/docs/database/web/structure-data).\n\nImplementation path\n\n|---|-----------------------------------------------|------------------------------------------------------------------------------------------------------------|\n| | Integrate the Firebase Realtime Database SDKs | Quickly include clients using Gradle, CocoaPods, or a script include. |\n| | Create Realtime Database References | Reference your JSON data, such as \"users/user:1234/phone_number\" to set data or subscribe to data changes. |\n| | Set Data and Listen for Changes | Use these references to write data or subscribe to changes. |\n| | Enable Offline Persistence | Allow data to be written to the device's local disk so it can be available while offline. |\n| | Secure your data | Use Firebase Realtime Database Security Rules to secure your data. |\n\nStore other types of data\n\n- [Cloud Firestore](/docs/firestore) is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud. To learn more about the differences between database options, see [Choose a database: Cloud Firestore or Realtime Database](/docs/firestore/rtdb-vs-firestore).\n- [Firebase Remote Config](/docs/remote-config) stores developer specified key-value pairs to change the behavior and appearance of your app without requiring users to download an update.\n- [Firebase Hosting](/docs/hosting) hosts the HTML, CSS, and JavaScript for your website as well as other developer-provided assets like graphics, fonts, and icons.\n- [Cloud Storage](/docs/storage) stores files such as images, videos, and audio as well as other user-generated content.\n\nNext steps:\n\n- Set data and listen for changes using the [Apple platforms](/docs/database/ios/start), [Android](/docs/database/android/start), [Web](/docs/database/web/start), [Admin](/docs/database/admin/start) SDKs, or the [REST API](/docs/database/rest/start).\n- Add Firebase Realtime Database to your [Apple](/docs/database/ios/start), [Android](/docs/database/android/start), or [Web](/docs/database/web/start) app.\n- Learn about how to secure your files using [Firebase Realtime Database Security Rules](/docs/database/security)."]]