DatabaseReference
public class DatabaseReference extends Query
| java.lang.Object | ||
| ↳ | com.google.firebase.database.Query | |
| ↳ | com.google.firebase.database.DatabaseReference |
A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location.
This class is the starting point for all Database operations. After you've initialized it with a URL, you can use it to read data, write data, and to create new DatabaseReferences.
Summary
Nested types |
|---|
public interface DatabaseReference.CompletionListenerThis interface is used as a method of being notified when an operation has been acknowledged by the Database servers and can be considered complete |
Public methods |
|
|---|---|
@NonNull DatabaseReference |
Get a reference to location relative to this one |
boolean |
|
@NonNull FirebaseDatabase |
Gets the Database instance associated with this reference. |
@Nullable String |
getKey() |
@Nullable DatabaseReference |
|
@NonNull DatabaseReference |
getRoot() |
static void |
Manually disconnect the Firebase Database client from the server and disable automatic reconnection. |
static void |
goOnline()Manually reestablish a connection to the Firebase Database server and enable automatic reconnection. |
int |
hashCode() |
@NonNull OnDisconnect |
Provides access to disconnect operations at this location |
@NonNull DatabaseReference |
push()Create a reference to an auto-generated child location. |
@NonNull Task<Void> |
Set the value at this location to 'null' |
void |
Set the value at this location to 'null' |
void |
runTransaction(@NonNull Transaction.Handler handler)Run a transaction on the data at this location. |
void |
runTransaction(Run a transaction on the data at this location. |
@NonNull Task<Void> |
setPriority(@Nullable Object priority)Set a priority for the data at this Database location. |
void |
setPriority(Set a priority for the data at this Database location. |
@NonNull Task<Void> |
Set the data at this location to the given value. |
void |
setValue(Set the data at this location to the given value. |
@NonNull Task<Void> |
Set the data and priority to the given values. |
void |
setValue(Set the data and priority to the given values. |
String |
toString() |
@NonNull Task<Void> |
updateChildren(@NonNull Map<String, Object> update)Update the specific child keys to the specified values. |
void |
updateChildren(Update the specific child keys to the specified values. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public methods
child
public @NonNull DatabaseReference child(@NonNull String pathString)
Get a reference to location relative to this one
| Parameters | |
|---|---|
@NonNull String pathString |
The relative path from this reference to the new one that should be created |
| Returns | |
|---|---|
@NonNull DatabaseReference |
A new DatabaseReference to the given path |
getDatabase
public @NonNull FirebaseDatabase getDatabase()
Gets the Database instance associated with this reference.
| Returns | |
|---|---|
@NonNull FirebaseDatabase |
The Database object for this reference. |
getParent
public @Nullable DatabaseReference getParent()
| Returns | |
|---|---|
@Nullable DatabaseReference |
A DatabaseReference to the parent location, or null if this instance references the root location |
getRoot
public @NonNull DatabaseReference getRoot()
| Returns | |
|---|---|
@NonNull DatabaseReference |
A reference to the root location of this Firebase Database |
goOffline
public static void goOffline()
Manually disconnect the Firebase Database client from the server and disable automatic reconnection.
Note: Invoking this method will impact all Firebase Database connections.
goOnline
public static void goOnline()
Manually reestablish a connection to the Firebase Database server and enable automatic reconnection.
Note: Invoking this method will impact all Firebase Database connections.
onDisconnect
public @NonNull OnDisconnect onDisconnect()
Provides access to disconnect operations at this location
| Returns | |
|---|---|
@NonNull OnDisconnect |
An object for managing disconnect operations at this location |
push
public @NonNull DatabaseReference push()
Create a reference to an auto-generated child location. The child key is generated client-side and incorporates an estimate of the server's time for sorting purposes. Locations generated on a single client will be sorted in the order that they are created, and will be sorted approximately in order across all clients.
| Returns | |
|---|---|
@NonNull DatabaseReference |
A DatabaseReference pointing to the new location |
removeValue
public void removeValue(@Nullable DatabaseReference.CompletionListener listener)
Set the value at this location to 'null'
| Parameters | |
|---|---|
@Nullable DatabaseReference.CompletionListener listener |
A listener that will be triggered when the operation is complete |
runTransaction
public void runTransaction(@NonNull Transaction.Handler handler)
Run a transaction on the data at this location. For more information on running transactions, see Transaction.Handler.
| Parameters | |
|---|---|
@NonNull Transaction.Handler handler |
An object to handle running the transaction |
runTransaction
public void runTransaction(
@NonNull Transaction.Handler handler,
boolean fireLocalEvents
)
Run a transaction on the data at this location. For more information on running transactions, see Transaction.Handler.
| Parameters | |
|---|---|
@NonNull Transaction.Handler handler |
An object to handle running the transaction |
boolean fireLocalEvents |
Defaults to true. If set to false, events will only be fired for the final result state of the transaction, and not for any intermediate states |
setPriority
public @NonNull Task<Void> setPriority(@Nullable Object priority)
Set a priority for the data at this Database location. Priorities can be used to provide a custom ordering for the children at a location (if no priorities are specified, the children are ordered by key). You cannot set a priority on an empty location. For this reason setValue(data, priority) should be used when setting initial data with a specific priority and setPriority should be used when updating the priority of existing data. Children are sorted based on this priority using the following rules:
- Children with no priority come first.
- Children with a number as their priority come next. They are sorted numerically by priority (small to large).
- Children with a string as their priority come last. They are sorted lexicographically by priority.
- Whenever two children have the same priority (including no priority), they are sorted by key. Numeric keys come first (sorted numerically), followed by the remaining keys (sorted lexicographically).
setPriority
public void setPriority(
@Nullable Object priority,
@Nullable DatabaseReference.CompletionListener listener
)
Set a priority for the data at this Database location. Priorities can be used to provide a custom ordering for the children at a location (if no priorities are specified, the children are ordered by key). You cannot set a priority on an empty location. For this reason setValue(data, priority) should be used when setting initial data with a specific priority and setPriority should be used when updating the priority of existing data. Children are sorted based on this priority using the following rules:
- Children with no priority come first.
- Children with a number as their priority come next. They are sorted numerically by priority (small to large).
- Children with a string as their priority come last. They are sorted lexicographically by priority.
- Whenever two children have the same priority (including no priority), they are sorted by key. Numeric keys come first (sorted numerically), followed by the remaining keys (sorted lexicographically).
| Parameters | |
|---|---|
@Nullable Object priority |
The priority to set at the specified location or null to clear the existing priority |
@Nullable DatabaseReference.CompletionListener listener |
A listener that will be triggered with results of the operation |
setValue
public @NonNull Task<Void> setValue(@Nullable Object value)
Set the data at this location to the given value. Passing null to setValue() will delete the data at the specified location. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleStringMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
setValue
public void setValue(
@Nullable Object value,
@Nullable DatabaseReference.CompletionListener listener
)
Set the data at this location to the given value. Passing null to setValue() will delete the data at the specified location. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleStringMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
| Parameters | |
|---|---|
@Nullable Object value |
The value to set at this location or null to delete the existing data |
@Nullable DatabaseReference.CompletionListener listener |
A listener that will be triggered with the results of the operation |
setValue
public @NonNull Task<Void> setValue(@Nullable Object value, @Nullable Object priority)
Set the data and priority to the given values. Passing null to setValue() will delete the data at the specified location. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleStringMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
setValue
public void setValue(
@Nullable Object value,
@Nullable Object priority,
@Nullable DatabaseReference.CompletionListener listener
)
Set the data and priority to the given values. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleStringMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
| Parameters | |
|---|---|
@Nullable Object value |
The value to set at this location or null to delete the existing data |
@Nullable Object priority |
The priority to set at this location or null to clear the existing priority |
@Nullable DatabaseReference.CompletionListener listener |
A listener that will be triggered with the results of the operation |
updateChildren
public @NonNull Task<Void> updateChildren(@NonNull Map<String, Object> update)
Update the specific child keys to the specified values. Passing null in a map to updateChildren() will remove the value at the specified location.
updateChildren
public void updateChildren(
@NonNull Map<String, Object> update,
@Nullable DatabaseReference.CompletionListener listener
)
Update the specific child keys to the specified values. Passing null in a map to updateChildren() will remove the value at the specified location.