Writing Data to Firebase


The CRUD operations on Firebase are given in the following slides: Firebase data is written to a Firebase Database reference and retrieved by attaching an asynchronous listener to the reference. The listener is triggered once for the initial state of the data and again anytime the data changes. To read or write data from the database, an instance of DatabaseReference is needed:

 private DatabaseReference mDatabase;
 // ...
 mDatabase = FirebaseDatabase.getInstance( ).getReference( );

For basic write operations, you can use setValue() to save data to a specified reference, replacing any existing data at that path. For example, the code below inserts a node called “app_title” in JSON top level:

 DatabaseReference mRef = mDatabase.getReference( "app_title" );
mRef.setValue( "Realtime Database" );

You can use this method setValue() to: