Sky Tracker
Sky Tracker

Reputation: 143

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

I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:

SELECT * FROM users WHERE type=admin

but only when the property total is changing. If I'm using:

users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
        for (DocumentChange dc : snapshots.getDocumentChanges()) {
            switch (dc.getType()) {
                case ADDED:
                    //Not trigger
                    break;
                case MODIFIED:
                    //Trigger
                    break;
                case REMOVED:
                    //
                    break;
            }
        }
    }
});

The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?

Please help me, is the last part of my project. How to skip is case ADDED?

Upvotes: 10

Views: 6738

Answers (4)

akshit tyagi
akshit tyagi

Reputation: 21

Found a work around for this given use case, it is possible to skip the initial data and get only updates. The workaround is including a server timestamp in your structure and build your query to fetch only the data that has timestamp greater than the current time.

val ref = db.collection("Messages").document("Client_ID")
        .collection("Private")
        .orderBy("timestamp")
        .whereGreaterThan("timestamp",Calendar.getInstance().time)

//Add snapshot listener
ref.addSnapshotListener { snapshot , e ->
            if (snapshot != null) {
                Log.d("TAG", "Current data: ${snapshot.documents.size}")
                for(document in snapshot.documents){
                    Log.e("Document Data",document.data.toString())
                }
            }
        }

So, the query won't return any data in the initial build, but will listen to the document changes. As soon as timestamp of a document changes, you'll be notified about that change. Then you can check if the data exists in your list (if you're looking for modifications, or if it's a new document added)

Just update timestamp of the document when your write any changes. As shown below :

val message = hashMapOf<String,Any>(
        "timestamp" to FieldValue.serverTimestamp(),
        "id" to userId,
        "data" to data,
    )
db.collection("Messages").document("Client_ID")
        .collection("Private")
        .document().set(message)

Upvotes: 0

Artur Gniewowski
Artur Gniewowski

Reputation: 500

There is an option to check if the querySnapshot is from a cache, changes return false

if(querySnapshot.getMetadata().isFromCache()) return

Upvotes: 5

Raj
Raj

Reputation: 996

Here is a solution working for me: use

AtomicBoolean isFirstListener = new AtomicBoolean(true);

and then on event method

if (isFirstListener.get()) {
                    isFirstListener.set(false);
                    //TODO Handle the entire list. 
                    return;
                }

Here is a sample code from my project:

 final AtomicBoolean isFirstListener = new AtomicBoolean(true);
 mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
    .addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e) {
        if (isFirstListener.get()) {
            isFirstListener.set(false);
            //TODO Handle the entire list.
            return;
        }
    }
});

reference: answer

Upvotes: 3

Alex Mamo
Alex Mamo

Reputation: 138969

When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:

Starts listening to this query.

Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.

Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.

According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.

Upvotes: 11

Related Questions