FirebaseFirestore
class FirebaseFirestore
Represents a Cloud Firestore database and is the entry point for all Cloud Firestore operations.
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 functions |
|
|---|---|
ListenerRegistration |
addSnapshotsInSyncListener(runnable: Runnable)Attaches a listener for a snapshots-in-sync event. |
ListenerRegistration |
addSnapshotsInSyncListener(activity: Activity, runnable: Runnable)Attaches a listener for a snapshots-in-sync event. |
ListenerRegistration |
addSnapshotsInSyncListener(executor: Executor, runnable: Runnable)Attaches a listener for a snapshots-in-sync event. |
WriteBatch |
batch()Creates a write batch, used for performing multiple writes as a single atomic operation. |
Task<Void!> |
Clears the persistent storage, including pending writes and cached documents. |
CollectionReference |
collection(collectionPath: String)Gets a |
Query |
collectionGroup(collectionId: String)Creates and returns a new |
Task<Void!> |
Disables network access for this instance. |
DocumentReference |
Gets a `DocumentReference` instance that refers to the document at the specified path within the database. |
Task<Void!> |
Re-enables network usage for this instance after a prior call to |
FirebaseApp |
getApp()Returns the FirebaseApp instance to which this |
FirebaseFirestoreSettings |
Returns the settings used by this |
java-static FirebaseFirestore |
Returns the default |
java-static FirebaseFirestore |
getInstance(app: FirebaseApp)Returns the default |
java-static FirebaseFirestore |
getInstance(database: String)Returns the |
java-static FirebaseFirestore |
getInstance(app: FirebaseApp, database: String)Returns the |
Task<Query!> |
getNamedQuery(name: String)Reads a Firestore |
LoadBundleTask |
loadBundle(bundleData: ByteArray)Loads a Firestore bundle into the local cache. |
LoadBundleTask |
loadBundle(bundleData: ByteBuffer)Loads a Firestore bundle into the local cache. |
LoadBundleTask |
loadBundle(bundleData: InputStream)Loads a Firestore bundle into the local cache. |
Task<Void!> |
runBatch(batchFunction: WriteBatch.Function)Executes a batchFunction on a newly created |
Task<TResult!> |
<TResult> runTransaction(updateFunction: Transaction.Function<TResult!>)Executes the given |
Task<TResult!> |
<TResult> runTransaction(Executes the given |
Unit |
setFirestoreSettings(settings: FirebaseFirestoreSettings)Sets any custom settings used to configure this |
Task<Void!> |
This function is deprecated. Instead of creating cache indexes manually, consider using |
java-static Unit |
setLoggingEnabled(loggingEnabled: Boolean)Globally enables / disables Cloud Firestore logging for the SDK. |
Task<Void!> |
Terminates this |
Unit |
useEmulator(host: String, port: Int)Modifies this FirebaseDatabase instance to communicate with the Cloud Firestore emulator. |
Task<Void!> |
Waits until all currently pending writes for the active user have been acknowledged by the backend. |
Public properties |
|
|---|---|
PersistentCacheIndexManager? |
Public functions
addSnapshotsInSyncListener
fun addSnapshotsInSyncListener(runnable: Runnable): ListenerRegistration
Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if a single server-generated change affects multiple listeners.
NOTE: The snapshots-in-sync event only indicates that listeners are in sync with each other, but does not relate to whether those snapshots are in sync with the server. Use SnapshotMetadata in the individual listeners to determine if a snapshot is from the cache or the server.
| Parameters | |
|---|---|
runnable: Runnable |
A callback to be called every time all snapshot listeners are in sync with each other. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotsInSyncListener
fun addSnapshotsInSyncListener(activity: Activity, runnable: Runnable): ListenerRegistration
Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if a single server-generated change affects multiple listeners.
NOTE: The snapshots-in-sync event only indicates that listeners are in sync with each other, but does not relate to whether those snapshots are in sync with the server. Use SnapshotMetadata in the individual listeners to determine if a snapshot is from the cache or the server.
| Parameters | |
|---|---|
activity: Activity |
The activity to scope the listener to. |
runnable: Runnable |
A callback to be called every time all snapshot listeners are in sync with each other. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotsInSyncListener
fun addSnapshotsInSyncListener(executor: Executor, runnable: Runnable): ListenerRegistration
Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if a single server-generated change affects multiple listeners.
NOTE: The snapshots-in-sync event only indicates that listeners are in sync with each other, but does not relate to whether those snapshots are in sync with the server. Use SnapshotMetadata in the individual listeners to determine if a snapshot is from the cache or the server.
| Parameters | |
|---|---|
executor: Executor |
The executor to use to call the listener. |
runnable: Runnable |
A callback to be called every time all snapshot listeners are in sync with each other. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
batch
fun batch(): WriteBatch
Creates a write batch, used for performing multiple writes as a single atomic operation.
The maximum number of writes allowed in a single batch is 500, but note that each usage of serverTimestamp, arrayUnion, arrayRemove, or increment inside a transaction counts as an additional write.
| Returns | |
|---|---|
WriteBatch |
The created WriteBatch object. |
clearPersistence
fun clearPersistence(): Task<Void!>
Clears the persistent storage, including pending writes and cached documents.
Must be called while the FirebaseFirestore instance is not started (after the app is shutdown or when the app is first initialized). On startup, this method must be called before other methods (other than setFirestoreSettings). If the FirebaseFirestore instance is still running, the Task will fail with an error code of FAILED_PRECONDITION.
Note: clearPersistence() is primarily intended to help write reliable tests that use Cloud Firestore. It uses an efficient mechanism for dropping existing data but does not attempt to securely overwrite or otherwise make cached data unrecoverable. For applications that are sensitive to the disclosure of cached data in between user sessions, we strongly recommend not enabling persistence at all.
collection
fun collection(collectionPath: String): CollectionReference
Gets a CollectionReference instance that refers to the collection at the specified path within the database.
| Parameters | |
|---|---|
collectionPath: String |
A slash-separated path to a collection. |
| Returns | |
|---|---|
CollectionReference |
The |
collectionGroup
fun collectionGroup(collectionId: String): Query
Creates and returns a new Query that includes all documents in the database that are contained in a collection or subcollection with the given collectionId.
| Parameters | |
|---|---|
collectionId: String |
Identifies the collections to query over. Every collection or subcollection with this ID as the last segment of its path will be included. Cannot contain a slash. |
| Returns | |
|---|---|
Query |
The created Query. |
disableNetwork
fun disableNetwork(): Task<Void!>
Disables network access for this instance. While the network is disabled, any snapshot listeners or get() calls will return results from cache, and any write operations will be queued until network usage is re-enabled via a call to enableNetwork.
document
fun document(documentPath: String): DocumentReference
Gets a `DocumentReference` instance that refers to the document at the specified path within the database.
| Parameters | |
|---|---|
documentPath: String |
A slash-separated path to a document. |
| Returns | |
|---|---|
DocumentReference |
The DocumentReference instance. |
enableNetwork
fun enableNetwork(): Task<Void!>
Re-enables network usage for this instance after a prior call to disableNetwork.
getApp
fun getApp(): FirebaseApp
Returns the FirebaseApp instance to which this FirebaseFirestore belongs.
getFirestoreSettings
fun getFirestoreSettings(): FirebaseFirestoreSettings
Returns the settings used by this FirebaseFirestore object.
getInstance
java-static fun getInstance(): FirebaseFirestore
Returns the default FirebaseFirestore instance for the default FirebaseApp.
Returns the same instance for all invocations. If no instance exists, initializes a new instance.
The FirebaseFirestore instance.
getInstance
java-static fun getInstance(app: FirebaseApp): FirebaseFirestore
Returns the default FirebaseFirestore instance for the provided FirebaseApp.
For a given FirebaseApp, invocation always returns the same instance. If no instance exists, initializes a new instance.
The FirebaseFirestore instance.
| Parameters | |
|---|---|
app: FirebaseApp |
The |
getInstance
java-static fun getInstance(database: String): FirebaseFirestore
Returns the FirebaseFirestore instance for the default FirebaseApp.
Returns the same instance for all invocations given the same database parameter. If no instance exists, initializes a new instance.
The FirebaseFirestore instance.
| Parameters | |
|---|---|
database: String |
The database ID. |
getInstance
java-static fun getInstance(app: FirebaseApp, database: String): FirebaseFirestore
Returns the FirebaseFirestore instance for the provided FirebaseApp.
Returns the same instance for all invocations given the same FirebaseApp and database parameter. If no instance exists, initializes a new instance.
The FirebaseFirestore instance.
| Parameters | |
|---|---|
app: FirebaseApp |
The |
database: String |
The database ID. |
getNamedQuery
fun getNamedQuery(name: String): Task<Query!>
Reads a Firestore Query from local cache, identified by the given name.
The named queries are packaged into bundles on the server side (along with resulting documents) and loaded to local cache using loadBundle. Once in local cache, you can use this method to extract a query by name.
loadBundle
fun loadBundle(bundleData: ByteArray): LoadBundleTask
Loads a Firestore bundle into the local cache.
| Parameters | |
|---|---|
bundleData: ByteArray |
A byte array representing the bundle to be loaded. |
| Returns | |
|---|---|
LoadBundleTask |
A |
loadBundle
fun loadBundle(bundleData: ByteBuffer): LoadBundleTask
Loads a Firestore bundle into the local cache.
| Parameters | |
|---|---|
bundleData: ByteBuffer |
A ByteBuffer representing the bundle to be loaded. |
| Returns | |
|---|---|
LoadBundleTask |
A |
loadBundle
fun loadBundle(bundleData: InputStream): LoadBundleTask
Loads a Firestore bundle into the local cache.
| Parameters | |
|---|---|
bundleData: InputStream |
A stream representing the bundle to be loaded. |
| Returns | |
|---|---|
LoadBundleTask |
A |
runBatch
fun runBatch(batchFunction: WriteBatch.Function): Task<Void!>
Executes a batchFunction on a newly created WriteBatch and then commits all of the writes made by the batchFunction as a single atomic unit.
| Parameters | |
|---|---|
batchFunction: WriteBatch.Function |
The function to execute within the batch context. |
runTransaction
fun <TResult> runTransaction(updateFunction: Transaction.Function<TResult!>): Task<TResult!>
Executes the given updateFunction and then attempts to commit the changes applied within the transaction. If any document read within the transaction has changed, the updateFunction will be retried. If it fails to commit after 5 attempts (the default failure limit), the transaction will fail. To have a different number of retries, use the runTransaction method instead.
| Parameters | |
|---|---|
updateFunction: Transaction.Function<TResult!> |
The function to execute within the transaction context. |
| Returns | |
|---|---|
Task<TResult!> |
The task returned from the updateFunction. |
runTransaction
fun <TResult> runTransaction(
options: TransactionOptions,
updateFunction: Transaction.Function<TResult!>
): Task<TResult!>
Executes the given updateFunction and then attempts to commit the changes applied within the transaction. If any document read within the transaction has changed, the updateFunction will be retried. If it fails to commit after the maxmimum number of attempts specified in transactionOptions, the transaction will fail.
| Parameters | |
|---|---|
options: TransactionOptions |
The transaction options for controlling execution. |
updateFunction: Transaction.Function<TResult!> |
The function to execute within the transaction context. |
| Returns | |
|---|---|
Task<TResult!> |
The task returned from the updateFunction. |
setFirestoreSettings
fun setFirestoreSettings(settings: FirebaseFirestoreSettings): Unit
Sets any custom settings used to configure this FirebaseFirestore object. This method can only be called before calling any other methods on this object.
setIndexConfiguration
@PreviewApi
funsetIndexConfiguration(json: String): Task<Void!>
Configures indexing for local query execution. Any previous index configuration is overridden. The Task resolves once the index configuration has been persisted.
The index entries themselves are created asynchronously. You can continue to use queries that require indexing even if the indices are not yet available. Query execution will automatically start using the index once the index entries have been written.
The method accepts the JSON format exported by the Firebase CLI (`firebase firestore:indexes`). If the JSON format is invalid, this method throws an exception.
| Parameters | |
|---|---|
json: String |
The JSON format exported by the Firebase CLI. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException |
if the JSON format is invalid |
setLoggingEnabled
java-static fun setLoggingEnabled(loggingEnabled: Boolean): Unit
Globally enables / disables Cloud Firestore logging for the SDK.
terminate
fun terminate(): Task<Void!>
Terminates this FirebaseFirestore instance.
After calling terminate() only the clearPersistence method may be used. Any other method will throw an IllegalStateException.
To restart after termination, simply create a new instance of FirebaseFirestore with getInstance or getInstance.
terminate() does not cancel any pending writes and any tasks that are awaiting a response from the server will not be resolved. The next time you start this instance, it will resume attempting to send these writes to the server.
Note: Under normal circumstances, calling terminate() is not required. This method is useful only when you want to force this instance to release all of its resources or in combination with clearPersistence to ensure that all local state is destroyed between test runs.
useEmulator
fun useEmulator(host: String, port: Int): Unit
Modifies this FirebaseDatabase instance to communicate with the Cloud Firestore emulator.
Note: Call this method before using the instance to do any database operations.
waitForPendingWrites
fun waitForPendingWrites(): Task<Void!>
Waits until all currently pending writes for the active user have been acknowledged by the backend.
The returned Task completes immediately if there are no outstanding writes. Otherwise, the Task waits for all previously issued writes (including those written in a previous app session), but it does not wait for writes that were added after the method is called. If you wish to wait for additional writes, you have to call waitForPendingWrites() again.
Any outstanding waitForPendingWrites() Tasks are cancelled during user changes.