0

i have a problem with my code. I want only to listen when the document is added or deleted. The code is working nearly good. Now when the doc is added - toast appear. That works fine. But when i look at the read counter in my database - it increasing so quickly, like the function is 100% time reading all the data from database - not only for changes, like i want. I need to listen only for the changes in collection. Is it a way to do this in android? I have a webapp and there my javascript code is working fine, read data only when the change appear.

Below my code:

public void listenToDocument() {
    // [START listen_document]
    myDatabase.collection("markery").addSnapshotListener(new EventListener<QuerySnapshot>()
    {

        @Override
        public void onEvent(@Nullable QuerySnapshot snapshots,
                            @Nullable FirebaseFirestoreException e) {

            if (e != null) {
                Log.w(TAG, "listen:error", e);
                return;
            }

            for (DocumentChange documentChange : snapshots.getDocumentChanges()) {
                switch (documentChange.getType()) {
                    case ADDED:
                        String popup_data = documentChange.getDocument().getData().get("popup_data").toString();
                        GeoPoint geop = (GeoPoint) documentChange.getDocument().getData().get("geop");
                        Context context = getApplicationContext();
                        Toast toast = Toast.makeText(context, popup_data, Toast.LENGTH_LONG);
                        toast.show();
                        double lng = geop.getLongitude();
                        double lat = geop.getLatitude();
                        symbolLayerIconFeatureList.add(Feature.fromGeometry(
                                Point.fromLngLat(lng, lat)));
                        break;
                    case MODIFIED:

                        break;
                    case REMOVED:

                        break;
                }
            }

        }
    });
    // [END listen_document]
}
Headsman
  • 25
  • 5
  • Do you have the document list open in the Firebase console? If so, keep in mind that any reads performed by the Firebase console also count towards your project usage and are (during early development) often more likely to drive up the read count than your actual code. – Frank van Puffelen Jul 12 '20 at 15:03

1 Answers1

0

When you add a listener to a query (or a collection reference, which is just a query for all documents in the collection), it will read all of the documents and deliver them all to your listener for the first invocation. There is no way to add a listener for only new and changed documents after some point in time.

If you want only new changes, you should add some sort of timestamp field that gets updates with each change, and filter your query using that timestamp, so that only newly modified documents will be delivered to the listener. For example:

myDatabase
    .collection("markery")
    .whereGreaterThan("timestamp", new Date())
    .addSnapshotListener(...)

It will be up to you to make sure that timestamp is updated to the current time along with every update to every document in that collection.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I will try to use the timestamp. But i have i question, why my counter is increasing even when there is no change in the collection. I understand that when the change is made i will get all the documents, not only added. But the function is reading doc all the time, not only when the change is made. – Headsman Jul 12 '20 at 15:56
  • Usage of the firestore console costs reads. https://stackoverflow.com/questions/56434008/firestore-unexpected-reads – Doug Stevenson Jul 12 '20 at 15:58
  • I dont count the firestore console. I know that costs. I have only 2 documents, and apk was working like 5-10 min and i have now 600 read counts. – Headsman Jul 12 '20 at 16:03
  • We can't see all the interactions with your database. You're going to have to narrow this down by eliminating everything except the specific bit of code that performs the listen, and controlling the specific updates. – Doug Stevenson Jul 12 '20 at 16:10