DatabaseReference

public class DatabaseReference extends Query


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

This 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
child(@NonNull String pathString)

Get a reference to location relative to this one

boolean
equals(Object other)
@NonNull FirebaseDatabase

Gets the Database instance associated with this reference.

@Nullable String
@Nullable DatabaseReference
@NonNull DatabaseReference
static void

Manually disconnect the Firebase Database client from the server and disable automatic reconnection.

static void

Manually reestablish a connection to the Firebase Database server and enable automatic reconnection.

int
@NonNull OnDisconnect

Provides access to disconnect operations at this location

@NonNull DatabaseReference

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

Run a transaction on the data at this location.

void
runTransaction(
    @NonNull Transaction.Handler handler,
    boolean fireLocalEvents
)

Run a transaction on the data at this location.

@NonNull Task<Void>

Set a priority for the data at this Database location.

void

Set a priority for the data at this Database location.

@NonNull Task<Void>

Set the data at this location to the given value.

void

Set the data at this location to the given value.

@NonNull Task<Void>
setValue(@Nullable Object value, @Nullable Object priority)

Set the data and priority to the given values.

void
setValue(
    @Nullable Object value,
    @Nullable Object priority,
    @Nullable DatabaseReference.CompletionListener listener
)

Set the data and priority to the given values.

String
@NonNull Task<Void>

Update the specific child keys to the specified values.

void

Update the specific child keys to the specified values.

Inherited methods

From com.google.firebase.database.Query
@NonNull ChildEventListener

Add a listener for child events occurring at this location.

void

Add a listener for a single change in the data at this location.

@NonNull ValueEventListener

Add a listener for changes in the data at this location.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
endAt(double value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
endAt(boolean value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query
endAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query
endAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query
endBefore(double value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query
endBefore(boolean value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query
endBefore(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query
endBefore(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query

Creates a query constrained to only return child nodes with the given value.

@NonNull Query
equalTo(double value)

Creates a query constrained to only return child nodes with the given value.

@NonNull Query
equalTo(boolean value)

Creates a query constrained to only return child nodes with the given value.

@NonNull Query

Creates a query constrained to only return the child node with the given key and value.

@NonNull Query
equalTo(double value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value.

@NonNull Query
equalTo(boolean value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value.

@NonNull Task<DataSnapshot>
get()

Gets the server values for this query.

@NonNull DatabaseReference
void
keepSynced(boolean keepSynced)

By calling `keepSynced(true)` on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location.

@NonNull Query
limitToFirst(int limit)

Creates a query with limit and anchor it to the start of the window.

@NonNull Query
limitToLast(int limit)

Creates a query with limit and anchor it to the end of the window.

@NonNull Query

Creates a query in which child nodes are ordered by the values of the specified path.

@NonNull Query

Creates a query in which child nodes are ordered by their keys.

@NonNull Query

Creates a query in which child nodes are ordered by their priorities.

@NonNull Query

Creates a query in which nodes are ordered by their value

void

Remove the specified listener from this location.

void

Remove the specified listener from this location.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAfter(double value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAfter(boolean value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query
startAfter(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query
startAfter(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAt(double value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAt(boolean value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

@NonNull Query
startAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

@NonNull Query
startAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

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

equals

public boolean equals(Object other)

getDatabase

public @NonNull FirebaseDatabase getDatabase()

Gets the Database instance associated with this reference.

Returns
@NonNull FirebaseDatabase

The Database object for this reference.

getKey

public @Nullable String getKey()
Returns
@Nullable String

The last token in the location pointed to by this reference or null if this reference points to the database root

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.

hashCode

public int hashCode()

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 @NonNull Task<VoidremoveValue()

Set the value at this location to 'null'

Returns
@NonNull Task<Void>

The Task for this operation.

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<VoidsetPriority(@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).
Note that numerical priorities are parsed and ordered as IEEE 754 double-precision floating-point numbers. Keys are always stored as strings and are treated as numeric only when they can be parsed as a 32-bit integer.
Parameters
@Nullable Object priority

The priority to set at the specified location or null to clear the existing priority

Returns
@NonNull Task<Void>

The Task for this operation.

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).
Note that numerical priorities are parsed and ordered as IEEE 754 double-precision floating-point numbers. Keys are always stored as strings and are treated as numeric only when they can be parsed as a 32-bit integer.
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<VoidsetValue(@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:

  • Boolean
  • Long
  • Double
  • String
  • Map<String, Object>
  • List<Object>
In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:
  1. The class must have a default constructor that takes no arguments
  2. 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
Generic collections of objects that satisfy the above constraints are also permitted, i.e. 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

Returns
@NonNull Task<Void>

The Task for this operation.

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:

  • Boolean
  • Long
  • Double
  • String
  • Map<String, Object>
  • List<Object>
In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:
  1. The class must have a default constructor that takes no arguments
  2. 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
Generic collections of objects that satisfy the above constraints are also permitted, i.e. 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<VoidsetValue(@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:

  • Boolean
  • Long
  • Double
  • String
  • Map<String, Object>
  • List<Object>
In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:
  1. The class must have a default constructor that takes no arguments
  2. 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
Generic collections of objects that satisfy the above constraints are also permitted, i.e. 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

Returns
@NonNull Task<Void>

The Task for this operation.

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:

  • Boolean
  • Long
  • Double
  • String
  • Map<String, Object>
  • List<Object>
In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:
  1. The class must have a default constructor that takes no arguments
  2. 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
Generic collections of objects that satisfy the above constraints are also permitted, i.e. 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

toString

public String toString()
Returns
String

The full location url for this reference

updateChildren

public @NonNull Task<VoidupdateChildren(@NonNull Map<StringObject> 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.

Parameters
@NonNull Map<StringObject> update

The paths to update and their new values

Returns
@NonNull Task<Void>

The Task for this operation.

updateChildren

public void updateChildren(
    @NonNull Map<StringObject> 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.

Parameters
@NonNull Map<StringObject> update

The paths to update and their new values

@Nullable DatabaseReference.CompletionListener listener

A listener that will be triggered with results of the operation