Class Firestore
A Cloud Firestore client over the REST API, in pure Java.
No native SDK, Gradle dependency or pod is involved. Requests carry the
Firebase ID token from a TokenProvider, so Firestore security rules see
the signed-in user. Paths are relative to the database root: cities is a
collection and cities/SF a document.
Firestore db = new Firestore("my-project");
db.setTokenProvider(new FirebaseAuthTokenProvider());
Map<String, Object> city = new HashMap<String, Object>();
city.put("name", "San Francisco");
city.put("population", 870000);
db.set("cities/SF", city);
db.get("cities/SF").ready(new SuccessCallback<DocumentSnapshot>() {
public void onSucess(DocumentSnapshot doc) {
Log.p("Name: " + doc.get("name"));
}
});
addSnapshotListener(String, SnapshotListener) and
addSnapshotListener(Query, SnapshotListener) deliver live updates.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a document with a server assigned ID.addSnapshotListener(Query query, SnapshotListener listener) Listens to the results of a query over a top-level collection.addSnapshotListener(String path, SnapshotListener listener) Listens to one document.commit(WriteBatch batch) Applies a batch of writes atomically.Deletes a document.Reads one document.Returns
Returns
Returns
Returns
Returns
Returns
Lists the documents of a collection, one page.static StringquoteFieldPath(String name) Quotes a field name for use in a field path.Runs a query over a top-level collection.Runs a query over a subcollection.Creates or replaces a document.voidSets the Web API key, sent as thekeyparameter.voidsetBaseUrl(String baseUrl) Points the client at another host, such as the local emulator (http://10.0.2.2:8080).voidsetTokenProvider(TokenProvider tokenProvider) Sets the token source.voidsetTransport(CloudHttpTransport transport) Replaces the HTTP transport.Changes only the given fields of an existing document.
-
Constructor Details
-
Firestore
Creates a client for the
(default)database.Parameters
projectId: the Firebase project ID
-
Firestore
-
-
Method Details
-
setTokenProvider
Sets the token source. Without one, requests are unauthenticated and only reach data that security rules leave public.
Parameters
tokenProvider: the token source, or null
-
setApiKey
Sets the Web API key, sent as the
keyparameter.Parameters
apiKey: the key, or null
-
setBaseUrl
Points the client at another host, such as the local emulator (
http://10.0.2.2:8080).Parameters
baseUrl: scheme, host and port without a trailing slash
-
getBaseUrl
Returns
the scheme, host and port requests go to
-
setTransport
Replaces the HTTP transport.
Parameters
transport: the transport
-
getTransport
Returns
the HTTP transport in use
-
getApiKey
Returns
the API key, or null
-
getTokenProvider
Returns
the token provider, or null
-
getDatabaseName
Returns
projects/<project>/databases/<database> -
getDocumentsRoot
Returns
projects/<project>/databases/<database>/documents -
get
Reads one document. A missing document completes with a snapshot whose
DocumentSnapshot.exists()is false.Parameters
path: the document path
Returns
the snapshot
-
list
Lists the documents of a collection, one page.
Parameters
-
collectionPath: the collection path -
pageSize: the maximum number of documents, 0 for the server default
Returns
the documents
-
-
add
Creates a document with a server assigned ID.
Parameters
-
collectionPath: the collection path -
data: the fields
Returns
the created document
-
-
set
Creates or replaces a document.
Parameters
-
path: the document path -
data: the full contents
Returns
the written document
-
-
update
Changes only the given fields of an existing document. The request fails with 404 when the document does not exist.
Parameters
-
path: the document path -
data: the fields to change
Returns
the written document
-
-
delete
Deletes a document. Deleting a missing document succeeds.
Parameters
path: the document path
Returns
true once deleted
-
runQuery
Runs a query over a top-level collection.
Parameters
query: the query
Returns
the matching documents in query order
-
runQuery
Runs a query over a subcollection.
Parameters
-
parentPath: the parent document path, such ascities/SF, or null for the root -
query: the query
Returns
the matching documents in query order
-
-
commit
Applies a batch of writes atomically.
Parameters
batch: the writes
Returns
true once committed
-
addSnapshotListener
Listens to one document. The first callback carries the current state and later ones every change.
Parameters
-
path: the document path -
listener: receives snapshots on the EDT
Returns
a handle that stops the listener
-
-
addSnapshotListener
Listens to the results of a query over a top-level collection.
Parameters
-
query: the query -
listener: receives snapshots on the EDT
Returns
a handle that stops the listener
-
-
quoteFieldPath
-