DocumentReference
public class DocumentReference
A DocumentReference refers to a document location in a Cloud Firestore database and can be used to write, read, or listen to the location. There may or may not exist a document at the referenced location. A DocumentReference can also be used to create a CollectionReference to a subcollection.
Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
Summary
Public fields |
|
|---|---|
final FirebaseFirestore |
Public methods |
|
|---|---|
@NonNull ListenerRegistration |
addSnapshotListener(@NonNull EventListener<DocumentSnapshot> listener)Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull ListenerRegistration |
addSnapshotListener(Starts listening to the document referenced by this |
@NonNull CollectionReference |
collection(@NonNull String collectionPath)Gets a |
@NonNull Task<Void> |
delete()Deletes the document referred to by this |
boolean |
|
@NonNull Task<DocumentSnapshot> |
get()Reads the document referenced by this |
@NonNull Task<DocumentSnapshot> |
Reads the document referenced by this |
@NonNull FirebaseFirestore |
Gets the Cloud Firestore instance associated with this document reference. |
@NonNull String |
getId() |
@NonNull CollectionReference |
Gets a |
@NonNull String |
getPath()Gets the path of this document (relative to the root of the database) as a slash-separated string. |
int |
hashCode() |
@NonNull Task<Void> |
Overwrites the document referred to by this |
@NonNull Task<Void> |
set(@NonNull Object data, @NonNull SetOptions options)Writes to the document referred to by this |
@NonNull Task<Void> |
Updates fields in the document referred to by this |
@NonNull Task<Void> |
Updates fields in the document referred to by this |
@NonNull Task<Void> |
Updates fields in the document referred to by this |
Extension functions |
|
|---|---|
final @NonNull Flow<T> |
<T extends Object> FirestoreKt.dataObjects(Starts listening to the document referenced by this |
final @NonNull Flow<@NonNull DocumentSnapshot> |
FirestoreKt.snapshots(Starts listening to the document referenced by this |
Public fields
Public methods
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(@NonNull EventListener<DocumentSnapshot> listener)
Starts listening to the document referenced by this DocumentReference.
| Parameters | |
|---|---|
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull Activity activity,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference using an Activity-scoped listener.
The listener will be automatically removed during onStop.
| Parameters | |
|---|---|
@NonNull Activity activity |
The activity to scope the listener to. |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull Executor executor,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference.
| Parameters | |
|---|---|
@NonNull Executor executor |
The executor to use to call the listener. |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull MetadataChanges metadataChanges,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference with the given options.
| Parameters | |
|---|---|
@NonNull MetadataChanges metadataChanges |
Indicates whether metadata-only changes (specifically, only |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull SnapshotListenOptions options,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference with the given options.
| Parameters | |
|---|---|
@NonNull SnapshotListenOptions options |
Sets snapshot listener options, including whether metadata-only changes should trigger snapshot events, the source to listen to, the executor to use to call the listener, or the activity to scope the listener to. |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull Activity activity,
@NonNull MetadataChanges metadataChanges,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference with the given options using an Activity-scoped listener.
The listener will be automatically removed during onStop.
| Parameters | |
|---|---|
@NonNull Activity activity |
The activity to scope the listener to. |
@NonNull MetadataChanges metadataChanges |
Indicates whether metadata-only changes (specifically, only |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
public @NonNull ListenerRegistration addSnapshotListener(
@NonNull Executor executor,
@NonNull MetadataChanges metadataChanges,
@NonNull EventListener<DocumentSnapshot> listener
)
Starts listening to the document referenced by this DocumentReference with the given options.
| Parameters | |
|---|---|
@NonNull Executor executor |
The executor to use to call the listener. |
@NonNull MetadataChanges metadataChanges |
Indicates whether metadata-only changes (specifically, only |
@NonNull EventListener<DocumentSnapshot> listener |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
@NonNull ListenerRegistration |
A registration object that can be used to remove the listener. |
collection
public @NonNull CollectionReference collection(@NonNull String collectionPath)
Gets a CollectionReference instance that refers to the subcollection at the specified path relative to this document.
| Returns | |
|---|---|
@NonNull CollectionReference |
The |
delete
public @NonNull Task<Void> delete()
Deletes the document referred to by this DocumentReference.
get
public @NonNull Task<DocumentSnapshot> get()
Reads the document referenced by this DocumentReference.
| Returns | |
|---|---|
@NonNull Task<DocumentSnapshot> |
A Task that will be resolved with the contents of the Document at this |
get
public @NonNull Task<DocumentSnapshot> get(@NonNull Source source)
Reads the document referenced by this DocumentReference.
By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.
| Returns | |
|---|---|
@NonNull Task<DocumentSnapshot> |
A Task that will be resolved with the contents of the Document at this |
getFirestore
public @NonNull FirebaseFirestore getFirestore()
Gets the Cloud Firestore instance associated with this document reference.
getParent
public @NonNull CollectionReference getParent()
Gets a CollectionReference to the collection that contains this document.
| Returns | |
|---|---|
@NonNull CollectionReference |
The |
getPath
public @NonNull String getPath()
Gets the path of this document (relative to the root of the database) as a slash-separated string.
set
public @NonNull Task<Void> set(@NonNull Object data)
Overwrites the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If a document already exists, it will be overwritten.
set
public @NonNull Task<Void> set(@NonNull Object data, @NonNull SetOptions options)
Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.
| Parameters | |
|---|---|
@NonNull Object data |
The data to write to the document (like a Map or a POJO containing the desired document contents). |
@NonNull SetOptions options |
An object to configure the set behavior. |
update
public @NonNull Task<Void> update(@NonNull Map<String, Object> data)
Updates fields in the document referred to by this DocumentReference. If no document exists yet, the update will fail.
update
public @NonNull Task<Void> update(
@NonNull String field,
@Nullable Object value,
Object[] moreFieldsAndValues
)
Updates fields in the document referred to by this DocumentReference. If no document exists yet, the update will fail.
update
public @NonNull Task<Void> update(
@NonNull FieldPath fieldPath,
@Nullable Object value,
Object[] moreFieldsAndValues
)
Updates fields in the document referred to by this DocumentReference. If no document exists yet, the update will fail.
Extension functions
FirestoreKt.dataObjects
public final @NonNull Flow<T> <T extends Object> FirestoreKt.dataObjects(
@NonNull DocumentReference receiver,
@NonNull MetadataChanges metadataChanges
)
Starts listening to the document referenced by this DocumentReference with the given options and emits its values converted to a POJO via a Flow.
-
When the returned flow starts being collected, an
EventListenerwill be attached. -
When the flow completes, the listener will be removed.
| Parameters | |
|---|---|
<T extends Object> |
The type of the object to convert to. |
@NonNull MetadataChanges metadataChanges |
controls metadata-only changes. Default: |
FirestoreKt.snapshots
public final @NonNull Flow<@NonNull DocumentSnapshot> FirestoreKt.snapshots(
@NonNull DocumentReference receiver,
@NonNull MetadataChanges metadataChanges
)
Starts listening to the document referenced by this DocumentReference with the given options and emits its values via a Flow.
-
When the returned flow starts being collected, an
EventListenerwill be attached. -
When the flow completes, the listener will be removed.
| Parameters | |
|---|---|
@NonNull MetadataChanges metadataChanges |
controls metadata-only changes. Default: |