Go to console

FirebaseFirestore

public class FirebaseFirestore extends Object

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.

Public Method Summary

WriteBatch
batch()
Creates a write batch, used for performing multiple writes as a single atomic operation.
Task<Void>
clearPersistence()
Clears the persistent storage, including pending writes and cached documents.
CollectionReference
collection(String collectionPath)
Gets a CollectionReference instance that refers to the collection at the specified path within the database.
Query
collectionGroup(String collectionId)
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.
Task<Void>
disableNetwork()
Disables network access for this instance.
DocumentReference
document(String documentPath)
Gets a `DocumentReference` instance that refers to the document at the specified path within the database.
Task<Void>
enableNetwork()
Re-enables network usage for this instance after a prior call to disableNetwork().
FirebaseApp
getApp()
Returns the FirebaseApp instance to which this FirebaseFirestore belongs.
FirebaseFirestoreSettings
getFirestoreSettings()
Returns the settings used by this FirebaseFirestore object.
static FirebaseFirestore
static FirebaseFirestore
Task<Void>
runBatch(WriteBatch.Function batchFunction)
Executes a batchFunction on a newly created WriteBatch and then commits all of the writes made by the batchFunction as a single atomic unit.
<TResult> Task<TResult>
runTransaction(Function<TResult> updateFunction)
Executes the given updateFunction and then attempts to commit the changes applied within the transaction.
void
setFirestoreSettings(FirebaseFirestoreSettings settings)
Sets any custom settings used to configure this FirebaseFirestore object.
static void
setLoggingEnabled(boolean loggingEnabled)
Globally enables / disables Cloud Firestore logging for the SDK.
Task<Void>
terminate()
Terminates this FirebaseFirestore instance.
Task<Void>
waitForPendingWrites()
Waits until all currently pending writes for the active user have been acknowledged by the backend.

Inherited Method Summary

Public Methods

public WriteBatch batch ()

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(Object), arrayRemove(Object), or increment(long) inside a transaction counts as an additional write.

Returns
  • The created WriteBatch object.

public Task<Void> clearPersistence ()

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(FirebaseFirestoreSettings)). 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.

Returns
  • A Task that is resolved when the persistent storage is cleared. Otherwise, the Task is rejected with an error.

public CollectionReference collection (String collectionPath)

Gets a CollectionReference instance that refers to the collection at the specified path within the database.

Parameters
collectionPath A slash-separated path to a collection.
Returns
  • The CollectionReference instance.

public Query collectionGroup (String collectionId)

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 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
  • The created Query.

public Task<Void> disableNetwork ()

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().

Returns
  • A Task that will be completed once networking is disabled.

public DocumentReference document (String documentPath)

Gets a `DocumentReference` instance that refers to the document at the specified path within the database.

Parameters
documentPath A slash-separated path to a document.
Returns
  • The DocumentReference instance.

public Task<Void> enableNetwork ()

Re-enables network usage for this instance after a prior call to disableNetwork().

Returns
  • A Task that will be completed once networking is enabled.

public FirebaseApp getApp ()

Returns the FirebaseApp instance to which this FirebaseFirestore belongs.

public FirebaseFirestoreSettings getFirestoreSettings ()

Returns the settings used by this FirebaseFirestore object.

public static FirebaseFirestore getInstance ()

public static FirebaseFirestore getInstance (FirebaseApp app)

public Task<Void> runBatch (WriteBatch.F