The Firebase Realtime Database stores and synchronizes data using a NoSQL cloud
database. Data is synchronized across all clients in realtime, and remains
available when your app goes offline.
Before You Begin
Before you can use
Firebase Realtime Database
,
you need to:
Register your C++ project and configure it to use Firebase.
If your C++ project already uses Firebase, then it's already registered and
configured for Firebase.
Add the
Firebase C++ SDK
to your C++ project.
Note that adding Firebase to your C++ project involves tasks both in the
Firebase console
and in your open C++ project (for example, you download
Firebase config files from the console, then move them into your C++ project).
Create a Database
Navigate to the
Realtime Database
section of the
Firebase console
.
You'll be prompted to select an existing Firebase project.
Follow the database creation workflow.
Select a starting mode for your Firebase Security Rules:
- Test mode
Good for getting started with the mobile and web client libraries,
but allows anyone to read and overwrite your data. After testing,
make
sure to review the
Understand Firebase Realtime Database Rules
section.
To get started with the web, Apple, or Android SDK, select testmode.
- Locked mode
Denies all reads and writes from mobile and web clients.
Your authenticated application servers can still access your database.
Choose a location for the database.
Depending on the
location of the database
, the
URL for the new database will be in one of the following forms:
Click
Done
.
When you enable Realtime Database, it also enables the API in the
Cloud API Manager
.
Create and Initialize firebase::App
Before you can access the Realtime Database, you'll need to create and initialize the
firebase::App
.
Include the header file for
firebase::App
:
#include "firebase/app.h"
Android
Create the
firebase::App
, passing the JNI environment and a
jobject
reference to the Java Activity as arguments:
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"), jni_env, activity);
iOS+
Create the
firebase::App
:
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"));
Access the firebase::database::Database Class
The
firebase::database::Database
is the entry point for the Firebase Realtime Database C++ SDK.
::firebase::database::Database *database = ::firebase::database::Database::GetInstance(app);
If you have chosen to use public access for your rules, you can proceed to the
sections on saving and retrieving data.
Setting up Restricted Access
If you do not want to use public access you can add Firebase Authentication to your
app to control access to the database.
Next Steps
Known Issues
- On desktop platforms (Windows, Mac, Linux), the Firebase C++ SDK uses
REST to access your database. Because of this, you must
declare the indexes you use
with Query::OrderByChild() on desktop or your listeners will fail.
- The desktop workflow version of Realtime Database does not support offline or
persistence.