Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to only read data from database when the document is added or deleted

Posted on November 14, 2022

Solution 1 :

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.

Problem :

This question already has answers here:

How to skip initial data and trigger only new updates in Firestore Firebase?

(3 answers)
Closed 2 years ago.

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.

READ  [ANSWERED] android - GoogleSignInClient account picker not showing after log out
Powered by Inline Related Posts

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]
}

Comments

Comment posted by Frank van Puffelen

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.

Comment posted by Headsman

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.

Comment posted by stackoverflow.com/questions/56434008/firestore-unexpected-reads

Usage of the firestore console costs reads.

Comment posted by Headsman

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.

Comment posted by Doug Stevenson

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.

READ  [ANSWERED] Why is the Android context menu empty?
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress