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

@NonNullDatabaseReference
child(@NonNullString pathString)

Get a reference to location relative to this one

boolean
equals(Object other)
@NonNullFirebaseDatabase

Gets the Database instance associated with this reference.

@NullableString
@NullableDatabaseReference
@NonNullDatabaseReference
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
@NonNullOnDisconnect

Provides access to disconnect operations at this location

@NonNullDatabaseReference

Create a reference to an auto-generated child location.

@NonNullTask<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(
    @NonNullTransaction.Handler handler,
    boolean fireLocalEvents
)

Run a transaction on the data at this location.

@NonNullTask<Void>

Set a priority for the data at this Database location.

void

Set a priority for the data at this Database location.

@NonNullTask<Void>

Set the data at this location to the given value.

void

Set the data at this location to the given value.

@NonNullTask<Void>
setValue(@NullableObject value, @NullableObject priority)

Set the data and priority to the given values.

void
setValue(
    @NullableObject value,
    @NullableObject priority,
    @NullableDatabaseReference.CompletionListener listener
)

Set the data and priority to the given values.

String
@NonNullTask<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
@NonNullChildEventListener

Add a listener for child events occurring at this location.

void

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

@NonNullValueEventListener

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

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
endAt(double value, @NullableString 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.

@NonNullQuery
endAt(boolean value, @NullableString 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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
endBefore(double value, @NullableString 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.

@NonNullQuery
endBefore(boolean value, @NullableString 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.

@NonNullQuery

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

@NonNullQuery
equalTo(double value)

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

@NonNullQuery
equalTo(boolean value)

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

@NonNullQuery

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

@NonNullQuery
equalTo(double value, @NullableString key)

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

@NonNullQuery
equalTo(boolean value, @NullableString key)

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

@NonNullTask<DataSnapshot>
get()

Gets the server values for this query.

@NonNullDatabaseReference
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.

@NonNullQuery
limitToFirst(int limit)

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

@NonNullQuery
limitToLast(int limit)

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

@NonNullQuery

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

@NonNullQuery

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

@NonNullQuery

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

@NonNullQuery

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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
startAfter(double value, @NullableString 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.

@NonNullQuery
startAfter(boolean value, @NullableString 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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
startAt(double value, @NullableString 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.

@NonNullQuery
startAt(boolean value, @NullableString 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 @NonNullDatabaseReference child(@NonNullString pathString)

Get a reference to location relative to this one

Parameters
@NonNullString pathString

The relative path from this reference to the new one that should be created

Returns
@NonNullDatabaseReference

A new DatabaseReference to the given path

equals

public boolean equals(Object other)

getDatabase

public @NonNullFirebaseDatabase getDatabase()

Gets the Database instance associated with this reference.

Returns
@NonNullFirebaseDatabase

The Database object for this reference.

getKey

public @NullableString getKey()
Returns
@NullableString

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

getParent

public @NullableDatabaseReference getParent()
Returns
@NullableDatabaseReference

A DatabaseReference to the parent location, or null if this instance references the root location

getRoot

public @NonNullDatabaseReference getRoot()
Returns
@NonNullDatabaseReference

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 @NonNullOnDisconnect onDisconnect()

Provides access to disconnect operations at this location

Returns
@NonNullOnDisconnect

An object for managing disconnect operations at this location

push

public @NonNullDatabaseReference 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
@NonNullDatabaseReference

A DatabaseReference pointing to the new location

removeValue

public @NonNullTask<VoidremoveValue()

Set the value at this location to 'null'

Returns
@NonNullTask<Void>

The Task for this operation.

removeValue

public void removeValue(@NullableDatabaseReference.CompletionListener listener)

Set the value at this location to 'null'

Parameters
@NullableDatabaseReference.CompletionListener listener

A listener that will be triggered when the operation is complete

runTransaction

public void runTransaction(@NonNullTransaction.Handler handler)

Run a transaction on the data at this location. For more information on running transactions, see Transaction.Handler.

Parameters
@NonNullTransaction.Handler handler

An object to handle running the transaction

runTransaction

public void runTransaction(
    @NonNullTransaction.Handler handler,
    boolean fireLocalEvents
)

Run a transaction on the data at this location. For more information on running transactions, see Transaction.Handler.

Parameters
@NonNullTransaction.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 @NonNullTask<VoidsetPriority(@NullableObject 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
@NullableObject priority

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

Returns
@NonNullTask<Void>

The Task for this operation.

setPriority

public void setPriority(
    @NullableObject priority,
    @NullableDatabaseReference.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
@NullableObject priority

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

@NullableDatabaseReference.CompletionListener listener

A listener that will be triggered with results of the operation

setValue

public @NonNullTask<VoidsetValue(@NullableObject 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
@NullableObject value

The value to set at this location or null to delete the existing data

Returns
@NonNullTask<Void>

The Task for this operation.

setValue

public void setValue(
    @NullableObject value,
    @NullableDatabaseReference.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
@NullableObject value

The value to set at this location or null to delete the existing data

@NullableDatabaseReference.CompletionListener listener

A listener that will be triggered with the results of the operation

setValue

public @NonNullTask<VoidsetValue(@NullableObject value, @NullableObject 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
@NullableObject value

The value to set at this location or null to delete the existing data

@NullableObject priority

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

Returns
@NonNullTask<Void>

The Task for this operation.

setValue

public void setValue(
    @NullableObject value,
    @NullableObject priority,
    @NullableDatabaseReference.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
@NullableObject value

The value to set at this location or null to delete the existing data

@NullableObject priority

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

@NullableDatabaseReference.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 @NonNullTask<VoidupdateChildren(@NonNullMap<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
@NonNullMap<StringObject> update

The paths to update and their new values

Returns
@NonNullTask<Void>

The Task for this operation.

updateChildren

public void updateChildren(
    @NonNullMap<StringObject> update,
    @NullableDatabaseReference.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
@NonNullMap<StringObject> update

The paths to update and their new values

@NullableDatabaseReference.CompletionListener listener

A listener that will be triggered with results of the operation