Query
public class Query
DatabaseReference |
A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. |
The Query class (and its subclass, DatabaseReference) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes. Instances of Query are obtained by calling startAt(), endAt(), or limit() on a DatabaseReference.
Summary
Public methods |
|
|---|---|
@NonNull ChildEventListener |
addChildEventListener(@NonNull ChildEventListener listener)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 |
addValueEventListener(@NonNull ValueEventListener listener)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 |
@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 |
@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 |
@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 |
@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 |
@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 |
@NonNull Query |
Creates a query constrained to only return child nodes with a value less than the given value, using the given |
@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 |
@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 |
@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 |
@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 |
@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 |
@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 |
Creates a query constrained to only return the child node with the given key and value. |
@NonNull Query |
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 |
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. |
@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 |
orderByChild(@NonNull String path)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 |
removeEventListener(@NonNull ChildEventListener listener)Remove the specified listener from this location. |
void |
removeEventListener(@NonNull ValueEventListener listener)Remove the specified listener from this location. |
@NonNull Query |
startAfter(@Nullable String value)Creates a query constrained to only return child nodes with a value greater than the given value, using the given |
@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 |
@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 |
@NonNull Query |
startAfter(@Nullable String value, @Nullable String key)Creates a query constrained to only return child nodes with a value greater or equal to the given value, using the given |
@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 |
@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 |
@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 |
@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 |
@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 |
@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 |
@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 |
@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 |
Extension functions |
|
|---|---|
final @NonNull Flow<@NonNull ChildEvent> |
DatabaseKt.getChildEvents(@NonNull Query receiver) |
final @NonNull Flow<@NonNull DataSnapshot> |
DatabaseKt.getSnapshots(@NonNull Query receiver) |
final @NonNull Flow<T> |
<T extends Object> DatabaseKt.values(@NonNull Query receiver)Starts listening to this query and emits its values converted to a POJO via a |
Public methods
addChildEventListener
public @NonNull ChildEventListener addChildEventListener(@NonNull ChildEventListener listener)
Add a listener for child events occurring at this location. When child locations are added, removed, changed, or moved, the listener will be triggered for the appropriate event
| Parameters | |
|---|---|
@NonNull ChildEventListener listener |
The listener to be called with changes |
| Returns | |
|---|---|
@NonNull ChildEventListener |
A reference to the listener provided. Save this to remove the listener later. |
addListenerForSingleValueEvent
public void addListenerForSingleValueEvent(@NonNull ValueEventListener listener)
Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.
| Parameters | |
|---|---|
@NonNull ValueEventListener listener |
The listener to be called with the data |
addValueEventListener
public @NonNull ValueEventListener addValueEventListener(@NonNull ValueEventListener listener)
Add a listener for changes in the data at this location. Each time the data changes, your listener will be called with an immutable snapshot of the data.
| Parameters | |
|---|---|
@NonNull ValueEventListener listener |
The listener to be called with changes |
| Returns | |
|---|---|
@NonNull ValueEventListener |
A reference to the listener provided. Save this to remove the listener later. |
endAt
public @NonNull Query endAt(@Nullable String 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.
endAt
public @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.
| Parameters | |
|---|---|
double value |
The value to end at, inclusive |
endAt
public @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.
2.0
| Parameters | |
|---|---|
boolean value |
The value to end at, inclusive |
endAt
public @NonNull Query endAt(@Nullable String 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.
endAt
public @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.
endAt
public @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.
2.0
endBefore
public @NonNull Query endBefore(@Nullable String 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.
19.6
endBefore
public @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.
19.6
| Parameters | |
|---|---|
double value |
The value to end at, exclusive |
endBefore
public @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.
19.6
| Parameters | |
|---|---|
boolean value |
The value to end at, exclusive |
endBefore
public @NonNull Query endBefore(@Nullable String 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.
19.6
endBefore
public @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.
19.6
endBefore
public @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.
19.6
equalTo
public @NonNull Query equalTo(@Nullable String value)
Creates a query constrained to only return child nodes with the given value.
equalTo
public @NonNull Query equalTo(double value)
Creates a query constrained to only return child nodes with the given value.
| Parameters | |
|---|---|
double value |
The value to query for |
equalTo
public @NonNull Query equalTo(boolean value)
Creates a query constrained to only return child nodes with the given value.
2.0
| Parameters | |
|---|---|
boolean value |
The value to query for |
equalTo
public @NonNull Query equalTo(@Nullable String value, @Nullable String key)
Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as names are unique.
equalTo
public @NonNull Query equalTo(double value, @Nullable String key)
Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.
equalTo
public @NonNull Query equalTo(boolean value, @Nullable String key)
Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.
get
public @NonNull Task<DataSnapshot> get()
Gets the server values for this query. Updates the cache and raises events if successful. If not connected, falls back to a locally-cached value.
getRef
public @NonNull DatabaseReference getRef()
| Returns | |
|---|---|
@NonNull DatabaseReference |
A DatabaseReference to this location |
keepSynced
public 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. Additionally, while a location is kept synced, it will not be evicted from the persistent disk cache.
2.3
| Parameters | |
|---|---|
boolean keepSynced |
Pass `true` to keep this location synchronized, pass `false` to stop synchronization. |
limitToFirst
public @NonNull Query limitToFirst(int limit)
Creates a query with limit and anchor it to the start of the window.
2.0
| Parameters | |
|---|---|
int limit |
The maximum number of child nodes to return |
limitToLast
public @NonNull Query limitToLast(int limit)
Creates a query with limit and anchor it to the end of the window.
2.0
| Parameters | |
|---|---|
int limit |
The maximum number of child nodes to return |
orderByChild
public @NonNull Query orderByChild(@NonNull String path)
Creates a query in which child nodes are ordered by the values of the specified path.
2.0
orderByKey
public @NonNull Query orderByKey()
Creates a query in which child nodes are ordered by their keys.
2.0
orderByPriority
public @NonNull Query orderByPriority()
Creates a query in which child nodes are ordered by their priorities.
2.0
orderByValue
public @NonNull Query orderByValue()
Creates a query in which nodes are ordered by their value
2.2
removeEventListener
public void removeEventListener(@NonNull ChildEventListener listener)
Remove the specified listener from this location.
| Parameters | |
|---|---|
@NonNull ChildEventListener listener |
The listener to remove |
removeEventListener
public void removeEventListener(@NonNull ValueEventListener listener)
Remove the specified listener from this location.
| Parameters | |
|---|---|
@NonNull ValueEventListener listener |
The listener to remove |
startAfter
public @NonNull Query startAfter(@Nullable String 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.
19.6
startAfter
public @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.
19.6
| Parameters | |
|---|---|
double value |
The value to start at, exclusive |
startAfter
public @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.
19.6
| Parameters | |
|---|---|
boolean value |
The value to start at, exclusive |
startAfter
public @NonNull Query startAfter(@Nullable String value, @Nullable String key)
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.
19.6
startAfter
public @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.
19.6
startAfter
public @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.
19.6
startAt
public @NonNull Query startAt(@Nullable String 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.
startAt
public @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.
| Parameters | |
|---|---|
double value |
The value to start at, inclusive |
startAt
public @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.
2.0
| Parameters | |
|---|---|
boolean value |
The value to start at, inclusive |
startAt
public @NonNull Query startAt(@Nullable String 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.
startAt
public @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.
startAt
public @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.
2.0
Extension functions
DatabaseKt.getChildEvents
public final @NonNull Flow<@NonNull ChildEvent> DatabaseKt.getChildEvents(@NonNull Query receiver)
DatabaseKt.getSnapshots
public final @NonNull Flow<@NonNull DataSnapshot> DatabaseKt.getSnapshots(@NonNull Query receiver)
DatabaseKt.values
public final @NonNull Flow<T> <T extends Object> DatabaseKt.values(@NonNull Query receiver)
Starts listening to this query and emits its values converted to a POJO via a Flow.
-
When the returned flow starts being collected, a
ValueEventListenerwill be attached. -
When the flow completes, the listener will be removed.