aloj
aloj

Reputation: 3284

Firestore not updating local database

I have an application that uses Firestore to save data to the server. I have configured offline persistence and get data using addSnapshotListener(). If I write something from my app, it is updated to server correctly, but on second device I can't see new data. If I get the document using get() data is updated but it is really slow. Is there anyway to update local database or get data from network if there is no changes to database?

Upvotes: 3

Views: 1738

Answers (1)

Julian Raj
Julian Raj

Reputation: 62

There are two types of data-model in firestore you can use.

  1. collection
  2. document

you need to use snapshot Listener based on the data-model you are trying to fetch. For instance:

for collection:

db.collection("collectionPath")
            .addSnapshotListener(EventListener<QuerySnapshot> { snapshot, e ->
                if (snapshot != null) {
                    for (change in snapshot.documentChanges) {
                        //read document from the change -> change.document
                    }
                }
            })

for document:

db.document("documentPath")
            .addSnapshotListener(EventListener<DocumentSnapshot>{ snapshot, e ->
                if (snapshot != null) {
                    //read fields from document -> snapshot.getString("fieldName")
                }
            })

Upvotes: 1

Related Questions