Summary: Nested Classes | Methods | Inherited Methods | [Expand All]
public class

# DatabaseReference

extends Query

java.lang.Object
↳ | com.google.firebase.database.Query
| ↳ | com.google.firebase.database.DatabaseReference

## Class Overview

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 Classes
interface | DatabaseReference.CompletionListener | 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
DatabaseReference | child(String pathString) Get a reference to location relative to this one
boolean | equals(Object other)
FirebaseDatabase | getDatabase() Gets the Database instance associated with this reference.
String | getKey()
DatabaseReference | getParent()
DatabaseReference | getRoot()
static void | goOffline() 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()
OnDisconnect | onDisconnect() Provides access to disconnect operations at this location
DatabaseReference | push() Create a reference to an auto-generated child location.
Task<Void> | removeValue() Set the value at this location to 'null'
void | removeValue(DatabaseReference.CompletionListener listener) Set the value at this location to 'null'
void | runTransaction(Transaction.Handler handler, boolean fireLocalEvents) Run a transaction on the data at this location.
void | runTransaction(Transaction.Handler handler) Run a transaction on the data at this location.
void | setPriority(Object priority, DatabaseReference.CompletionListener listener) Set a priority for the data at this Database location.
Task<Void> | setPriority(Object priority) Set a priority for the data at this Database location.
void | setValue(Object value, Object priority, DatabaseReference.CompletionListener listener) Set the data and priority to the given values.
void | setValue(Object value, DatabaseReference.CompletionListener listener) Set the data at this location to the given value.
Task<Void> | setValue(Object value, Object priority) Set the data and priority to the given values.
Task<Void> | setValue(Object value) Set the data at this location to the given value.
String | toString()
Task<Void> | updateChildren(Map<String, Object> update) Update the specific child keys to the specified values.
void | updateChildren(Map<String, Object> update, DatabaseReference.CompletionListener listener) Update the specific child keys to the specified values.

[Expand] Inherited Methods
From class com.google.firebase.database.Query
ChildEventListener | addChildEventListener(ChildEventListener listener) Add a listener for child events occurring at this location.
void | addListenerForSingleValueEvent(ValueEventListener listener) Add a listener for a single change in the data at this location.
ValueEventListener | addValueEventListener(ValueEventListener listener) Add a listener for changes in the data at this location.
Query | endAt(String value, String key) Create 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 key less than or equal to the given key.
Query | endAt(String value) Create 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.
Query | endAt(double value, String key) Create 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.
Query | endAt(double value) Create 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.
Query | endAt(boolean value) Create 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.
Query | endAt(boolean value, String key) Create 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.
Query | equalTo(String value, String key) Create a query constrained to only return the child node with the given key and value.
Query | equalTo(double value) Create a query constrained to only return child nodes with the given value
Query | equalTo(double value, String key) Create a query constrained to only return the child node with the given key and value.
Query | equalTo(String value) Create a query constrained to only return child nodes with the given value
Query | equalTo(boolean value, String key) Create a query constrained to only return the child node with the given key and value.
Query | equalTo(boolean value) Create a query constrained to only return child nodes with the given value.
DatabaseReference | getRef()
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.
Query | limitToFirst(int limit) Create a query with limit and anchor it to the start of the window
Query | limitToLast(int limit) Create a query with limit and anchor it to the end of the window
Query | orderByChild(String path) Create a query in which child nodes are ordered by the values of the specified path.
Query | orderByKey() Create a query in which child nodes are ordered by their keys.
Query | orderByPriority() Create a query in which child nodes are ordered by their priorities.
Query | orderByValue() Create a query in which nodes are ordered by their value
void | removeEventListener(ChildEventListener listener) Remove the specified listener from this location.
void | removeEventListener(ValueEventListener listener) Remove the specified listener from this location.
Query | startAt(double value) Create 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.
Query | startAt(String value, String key) Create 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.
Query | startAt(String value) Create 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.
Query | startAt(double value, String key) Create 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.
Query | startAt(boolean value) Create 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.
Query | startAt(boolean value, String key) Create 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.
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## Public Methods

#### public DatabaseReference child (String pathString)

Get a reference to location relative to this one

##### Parameters

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

##### Returns

- A new DatabaseReference to the given path

#### public boolean equals (Object other)

#### public FirebaseDatabase getDatabase ()

Gets the Database instance associated with this reference.

##### Returns

- The Database object for this reference.

#### public String getKey ()

##### Returns

- The last token in the location pointed to by this reference

#### public DatabaseReference getParent ()

##### Returns

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

#### public DatabaseReference getRoot ()

##### Returns

- A reference to the root location of this Firebase Database

#### 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.

#### 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.

#### public int hashCode ()

#### public OnDisconnect onDisconnect ()

Provides access to disconnect operations at this location

##### Returns

- An object for managing disconnect operations at this location

#### public 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

- A DatabaseReference pointing to the new location

#### public Task<Void> removeValue ()

Set the value at this location to 'null'

##### Returns

- The `Task` for this operation.

#### public void removeValue (DatabaseReference.CompletionListener listener)

Set the value at this location to 'null'

##### Parameters

listener | A listener that will be triggered when the operation is complete

#### public void runTransaction (Transaction.Handler handler, boolean fireLocalEvents)

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

##### Parameters

handler | An object to handle running the transaction
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

#### public void runTransaction (Transaction.Handler handler)

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

##### Parameters

handler | An object to handle running the transaction

#### public void setPriority (Object priority, 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

priority | The priority to set at the specified location.
listener | A listener that will be triggered with results of the operation

#### public Task<Void> setPriority (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

priority | The priority to set at the specified location.

##### Returns

- The `Task` for this operation.

#### public void setValue (Object value, Object priority, 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
- 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

value | The value to set at this location
priority | The priority to set at this location
listener | A listener that will be triggered with the results of the operation

#### public void setValue (Object value, 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
- 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

value | The value to set at this location
listener | A listener that will be triggered with the results of the operation

#### public Task<Void> setValue (Object value, 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
- 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

value | The value to set at this location
priority | The priority to set at this location

##### Returns

- The `Task` for this operation.

#### public Task<Void> setValue (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
- 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

value | The value to set at this location

##### Returns

- The `Task` for this operation.

#### public String toString ()

##### Returns

- The full location url for this reference

#### public Task<Void> updateChildren (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.

##### Parameters

update | The paths to update and their new values

##### Returns

- The `Task` for this operation.

#### public void updateChildren (Map<String, Object> update, 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

update | The paths to update and their new values
listener | A listener that will be triggered with results of the operation