firebase::database::Database

#include <database.h>

Entry point for the Firebase Realtime Database C++ SDK.

Summary

To use the SDK, call firebase::database::Database::GetInstance() to obtain an instance of Database, then use GetReference() to obtain references to child paths within the database. From there you can set data via Query::SetValue(), get data via Query::GetValue(), attach listeners, and more.

Constructors and Destructors

~Database()
Destructor for the Database object.

Public static functions

GetInstance(::firebase::App *app, InitResult *init_result_out)
Get an instance of Database corresponding to the given App.
GetInstance(::firebase::App *app, const char *url, InitResult *init_result_out)
Gets an instance of FirebaseDatabase for the specified URL.

Public functions

GetReference() const
Get a DatabaseReference to the root of the database.
GetReference(const char *path) const
Get a DatabaseReference for the specified path.
GetReferenceFromUrl(const char *url) const
Get a DatabaseReference for the provided URL, which must belong to the database URL this instance is already connected to.
GoOffline()
void
Shuts down the connection to the Firebase Realtime Database backend until GoOnline() is called.
GoOnline()
void
Resumes the connection to the Firebase Realtime Database backend after a previous GoOffline() call.
PurgeOutstandingWrites()
void
Purge all pending writes to the Firebase Realtime Database server.
app() const
App *
Get the firebase::App that this Database was created with.
set_persistence_enabled(bool enabled)
void
Sets whether pending write data will persist between application exits.
url() const
const char *
Get the URL that this Database was created with.

Public static functions

GetInstance

Database * GetInstance(
  ::firebase::App *app,
  InitResult *init_result_out
)

Get an instance of Database corresponding to the given App.

Firebase Realtime Database uses firebase::App to communicate with Firebase Authentication to authenticate users to the Database server backend.

If you call GetInstance() multiple times with the same App, you will get the same instance of Database.

Details
Parameters
app
Your instance of firebase::App. Firebase Realtime Database will use this to communicate with Firebase Authentication.
init_result_out
Optional: If provided, write the init result here. Will be set to kInitResultSuccess if initialization succeeded, or kInitResultFailedMissingDependency on Android if Google Play services is not available on the current device.
Returns
An instance of Database corresponding to the given App.

GetInstance

Database * GetInstance(
  ::firebase::App *app,
  const char *url,
  InitResult *init_result_out
)

Gets an instance of FirebaseDatabase for the specified URL.

If you call GetInstance() multiple times with the same App and URL, you will get the same instance of Database.

Details
Parameters
app
Your instance of firebase::App. Firebase Realtime Database will use this to communicate with Firebase Authentication.
url
The URL of your Firebase Realtime Database. This overrides any url specified in the App options.
init_result_out
Optional: If provided, write the init result here. Will be set to kInitResultSuccess if initialization succeeded, or kInitResultFailedMissingDependency on Android if Google Play services is not available on the current device.
Returns
An instance of Database corresponding to the given App and URL.

Public functions

GetReference

DatabaseReference GetReference() const 

Get a DatabaseReference to the root of the database.

Details
Returns
A DatabaseReference to the root of the database.

GetReference

DatabaseReference GetReference(
  const char *path
) const 

Get a DatabaseReference for the specified path.

Details
Returns
A DatabaseReference to the specified path in the database. If you specified an invalid path, the reference's DatabaseReference::IsValid() will return false.

GetReferenceFromUrl

DatabaseReference GetReferenceFromUrl(
  const char *url
) const 

Get a DatabaseReference for the provided URL, which must belong to the database URL this instance is already connected to.

Details
Returns
A DatabaseReference to the specified path in the database. If you specified an invalid path, the reference's DatabaseReference::IsValid() will return false.

GoOffline

void GoOffline()

Shuts down the connection to the Firebase Realtime Database backend until GoOnline() is called.

GoOnline

void GoOnline()

Resumes the connection to the Firebase Realtime Database backend after a previous GoOffline() call.

PurgeOutstandingWrites

void PurgeOutstandingWrites()

Purge all pending writes to the Firebase Realtime Database server.

The Firebase Realtime Database client automatically queues writes and sends them to the server at the earliest opportunity, depending on network connectivity. In some cases (e.g. offline usage) there may be a large number of writes waiting to be sent. Calling this method will purge all outstanding writes so they are abandoned. All writes will be purged, including transactions and onDisconnect() writes. The writes will be rolled back locally, perhaps triggering events for affected event listeners, and the client will not (re-)send them to the Firebase backend.

app

App * app() const 

Get the firebase::App that this Database was created with.

Details
Returns
The firebase::App this Database was created with.

set_persistence_enabled

void set_persistence_enabled(
  bool enabled
)

Sets whether pending write data will persist between application exits.

The Firebase Database client will cache synchronized data and keep track of all writes you've initiated while your application is running. It seamlessly handles intermittent network connections and re-sends write operations when the network connection is restored. However by default your write operations and cached data are only stored in-memory and will be lost when your app restarts. By setting this value to true, the data will be persisted to on-device (disk) storage and will thus be available again when the app is restarted (even when there is no network connectivity at that time).

Note:SetPersistenceEnabled should be called before creating any instances of DatabaseReference, and only needs to be called once per application.

Details
Parameters
enabled
Set this to true to persist write data to on-device (disk) storage, or false to discard pending writes when the app exists.

url

const char * url() const 

Get the URL that this Database was created with.

Details
Returns
The URL this Database was created with, or an empty string if this Database was created with default parameters. This string will remain valid in memory for the lifetime of this Database.

~Database

 ~Database()

Destructor for the Database object.

When deleted, this instance will be removed from the cache of Database objects. If you call GetInstance() in the future with the same App, a new Database instance will be created.

Send feedback about...

Need help? Visit our support page.