firebase::database::Database

#include <database.h>

Entry point for the Firebase RealtimeDatabase C++ SDK.

Summary

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

Constructors and Destructors

~Database()
Destructor for theDatabase object.

Public static functions

GetInstance(::firebase::App *app,InitResult *init_result_out)
Get an instance ofDatabase corresponding to the givenApp.
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 aDatabaseReference to the root of the database.
GetReference(const char *path) const
Get aDatabaseReference for the specified path.
GetReferenceFromUrl(const char *url) const
Get aDatabaseReference 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 RealtimeDatabase backend untilGoOnline() is called.
GoOnline()
void
Resumes the connection to the Firebase RealtimeDatabase backend after a previousGoOffline() call.
PurgeOutstandingWrites()
void
Purge all pending writes to the Firebase RealtimeDatabase server.
app() const
App *
Get thefirebase::App that thisDatabase was created with.
log_level() const
Get the log verbosity of thisDatabase instance.
set_log_level(LogLevel log_level)
void
Set the log verbosity of thisDatabase instance.
set_persistence_enabled(bool enabled)
void
Sets whether pending write data will persist between application exits.
url() const
const char *
Get the URL that thisDatabase was created with.

Public static functions

GetInstance

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

Get an instance ofDatabase corresponding to the givenApp.

Firebase RealtimeDatabase usesfirebase::App to communicate with Firebase Authentication to authenticate users to theDatabase server backend.

If you callGetInstance() multiple times with the sameApp, you will get the same instance ofDatabase.

Details
Parameters
app
Your instance offirebase::App. Firebase RealtimeDatabase 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 ofDatabase corresponding to the givenApp.

GetInstance

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

Gets an instance of FirebaseDatabase for the specified URL.

If you callGetInstance() multiple times with the sameApp and URL, you will get the same instance ofDatabase.

Details
Parameters
app
Your instance offirebase::App. Firebase RealtimeDatabase will use this to communicate with Firebase Authentication.
url
The URL of your Firebase RealtimeDatabase. This overrides any url specified in theApp 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 ofDatabase corresponding to the givenApp and URL.

Public functions

GetReference

DatabaseReferenceGetReference()const

Get aDatabaseReference to the root of the database.

Details
Returns
ADatabaseReference to the root of the database.

GetReference

DatabaseReferenceGetReference(constchar*path)const

Get aDatabaseReference for the specified path.

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

GetReferenceFromUrl

DatabaseReferenceGetReferenceFromUrl(constchar*url)const

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

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

GoOffline

voidGoOffline()

Shuts down the connection to the Firebase RealtimeDatabase backend untilGoOnline() is called.

GoOnline

voidGoOnline()

Resumes the connection to the Firebase RealtimeDatabase backend after a previousGoOffline() call.

PurgeOutstandingWrites

voidPurgeOutstandingWrites()

Purge all pending writes to the Firebase RealtimeDatabase server.

The Firebase RealtimeDatabase 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 thefirebase::App that thisDatabase was created with.

Details
Returns
Thefirebase::App thisDatabase was created with.

log_level

LogLevellog_level()const

Get the log verbosity of thisDatabase instance.

Details
Returns
Get the currently configured logging verbosity.

set_log_level

voidset_log_level(LogLevellog_level)

Set the log verbosity of thisDatabase instance.

The log filtering is cumulative with FirebaseApp. That is, this library's log messages will only be displayed if they are not filtered out by this library's log level setting and by FirebaseApp's log level setting.

Note: On Android this can only be set before any operations have been performed with the object.

Details
Parameters
log_level
Log level, by default this is set to kLogLevelInfo.

set_persistence_enabled

voidset_persistence_enabled(boolenabled)

Sets whether pending write data will persist between application exits.

The FirebaseDatabase 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 totrue, 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 ofDatabaseReference, 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

constchar*url()const

Get the URL that thisDatabase was created with.

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

~Database

~Database()

Destructor for theDatabase object.

When deleted, this instance will be removed from the cache ofDatabase objects. If you callGetInstance() in the future with the sameApp, a newDatabase instance will be created.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-01-23 UTC.