Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class Firestore

  1. Object
  2. 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.

Constructors

public Firestore(String projectId)Creates a client for the (default) database.
public Firestore(String projectId, String databaseId)Creates a client for a named database.

Methods

public void setTokenProvider(TokenProvider tokenProvider)Sets the token source.
public void setApiKey(String apiKey)Sets the Web API key, sent as the key parameter.
public void setBaseUrl(String baseUrl)Points the client at another host, such as the local emulator (http://10.0.2.2:8080).
public String getBaseUrl()
public void setTransport(CloudHttpTransport transport)Replaces the HTTP transport.
public CloudHttpTransport getTransport()
public String getApiKey()
public TokenProvider getTokenProvider()
public String getDatabaseName()
public String getDocumentsRoot()
public AsyncResource<DocumentSnapshot> get(String path)Reads one document.
public AsyncResource<List<DocumentSnapshot>> list(String collectionPath, int pageSize)Lists the documents of a collection, one page.
public AsyncResource<DocumentSnapshot> add(String collectionPath, Map<String, Object> data)Creates a document with a server assigned ID.
public AsyncResource<DocumentSnapshot> set(String path, Map<String, Object> data)Creates or replaces a document.
public AsyncResource<DocumentSnapshot> update(String path, Map<String, Object> data)Changes only the given fields of an existing document.
public AsyncResource<Boolean> delete(String path)Deletes a document.
public AsyncResource<List<DocumentSnapshot>> runQuery(Query query)Runs a query over a top-level collection.
public AsyncResource<List<DocumentSnapshot>> runQuery(String parentPath, Query query)Runs a query over a subcollection.
public AsyncResource<Boolean> commit(WriteBatch batch)Applies a batch of writes atomically.
public ListenerRegistration addSnapshotListener(String path, SnapshotListener listener)Listens to one document.
public ListenerRegistration addSnapshotListener(Query query, SnapshotListener listener)Listens to the results of a query over a top-level collection.
public static String quoteFieldPath(String name)Quotes a field name for use in a field path.

Inherited methods

Constructor details

Firestore

public Firestore(String projectId)
Creates a client for the (default) database.

Parameters

projectId String
the Firebase project ID

Firestore

public Firestore(String projectId, String databaseId)
Creates a client for a named database.

Parameters

projectId String
the Firebase project ID
databaseId String
the database ID

Method details

setTokenProvider

public void setTokenProvider(TokenProvider tokenProvider)
Sets the token source. Without one, requests are unauthenticated and only reach data that security rules leave public.

Parameters

tokenProvider TokenProvider
the token source, or null

setApiKey

public void setApiKey(String apiKey)
Sets the Web API key, sent as the key parameter.

Parameters

apiKey String
the key, or null

setBaseUrl

public void setBaseUrl(String baseUrl)
Points the client at another host, such as the local emulator (http://10.0.2.2:8080).

Parameters

baseUrl String
scheme, host and port without a trailing slash

getBaseUrl

public String getBaseUrl()

Returns

the scheme, host and port requests go to

setTransport

public void setTransport(CloudHttpTransport transport)
Replaces the HTTP transport.

Parameters

transport CloudHttpTransport
the transport

getTransport

public CloudHttpTransport getTransport()

Returns

the HTTP transport in use

getApiKey

public String getApiKey()

Returns

the API key, or null

getTokenProvider

public TokenProvider getTokenProvider()

Returns

the token provider, or null

getDatabaseName

public String getDatabaseName()

Returns

projects/<project>/databases/<database>

getDocumentsRoot

public String getDocumentsRoot()

Returns

projects/<project>/databases/<database>/documents

get

public AsyncResource<DocumentSnapshot> get(String path)
Reads one document. A missing document completes with a snapshot whose DocumentSnapshot.exists() is false.

Parameters

path String
the document path

Returns

the snapshot

list

public AsyncResource<List<DocumentSnapshot>> list(String collectionPath, int pageSize)
Lists the documents of a collection, one page.

Parameters

collectionPath String
the collection path
pageSize int
the maximum number of documents, 0 for the server default

Returns

the documents

add

public AsyncResource<DocumentSnapshot> add(String collectionPath, Map<String, Object> data)
Creates a document with a server assigned ID.

Parameters

collectionPath String
the collection path
data Map<String, Object>
the fields

Returns

the created document

set

public AsyncResource<DocumentSnapshot> set(String path, Map<String, Object> data)
Creates or replaces a document.

Parameters

path String
the document path
data Map<String, Object>
the full contents

Returns

the written document

update

public AsyncResource<DocumentSnapshot> update(String path, Map<String, Object> data)
Changes only the given fields of an existing document. The request fails with 404 when the document does not exist.

Parameters

path String
the document path
data Map<String, Object>
the fields to change

Returns

the written document

delete

public AsyncResource<Boolean> delete(String path)
Deletes a document. Deleting a missing document succeeds.

Parameters

path String
the document path

Returns

true once deleted

runQuery

public AsyncResource<List<DocumentSnapshot>> runQuery(Query query)
Runs a query over a top-level collection.

Parameters

query Query
the query

Returns

the matching documents in query order

runQuery

public AsyncResource<List<DocumentSnapshot>> runQuery(String parentPath, Query query)
Runs a query over a subcollection.

Parameters

parentPath String
the parent document path, such as cities/SF, or null for the root
query Query
the query

Returns

the matching documents in query order

commit

public AsyncResource<Boolean> commit(WriteBatch batch)
Applies a batch of writes atomically.

Parameters

batch WriteBatch
the writes

Returns

true once committed

addSnapshotListener

public ListenerRegistration addSnapshotListener(String path, SnapshotListener listener)
Listens to one document. The first callback carries the current state and later ones every change.

Parameters

path String
the document path
listener SnapshotListener
receives snapshots on the EDT

Returns

a handle that stops the listener

addSnapshotListener

public ListenerRegistration addSnapshotListener(Query query, SnapshotListener listener)
Listens to the results of a query over a top-level collection.

Parameters

query Query
the query
listener SnapshotListener
receives snapshots on the EDT

Returns

a handle that stops the listener

quoteFieldPath

public static String quoteFieldPath(String name)
Quotes a field name for use in a field path. Simple identifiers stay as they are; anything else is wrapped in backticks.

Parameters

name String
a single field name

Returns

the field path segment