Home > Enterprise >  Event Listener in Firestore doesn’t see field updates in documents inside collection. Android, Java
Event Listener in Firestore doesn’t see field updates in documents inside collection. Android, Java

Time:05-17

I have next firestore scheme: collection "users" - document user_id - collection "trips" - document trip_id - collection "requests" - document request_user_id with fields: and boolean field <is_accepted>

On one of my pages I want to listen to realtime updates of requests: if <is_accepted> changes from 0 to 1(it means user is accepted for a trip).

I have written EventListener to track it:

FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
            firebaseFirestore
                    .collection("users")
                    .document(user_id)
                    .collection("trip")
                    .document(trip_id)
                    .collection("requests")
                    .whereEqualTo("accepted", 1)
                    .orderBy("timestamp", Query.Direction.DESCENDING)
                    .addSnapshotListener(new EventListener<QuerySnapshot>() {
                        @Override
                        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                            Log.d("Hello", "Triggered");
                            if (error == null) {
                                if (value != null) {///  some actions}}

It works fine when to read data first time on the page but when I make updates in document like:

HashMap<String, Object> hashMap = new HashMap<String, Object>();
                hashMap.put("timestamp", Timestamp.now());
                hashMap.put("accepted", 1);

                firebaseFirestore
                        .collection("users")
                        .document(user_id)
                        .collection("trips")
                        .document(trip_id)
                        .collection("requests")
                        .document(joined_user_id)
                        .update(hashMap);

Listener doesn’t see any changes. What can be wrong?

CodePudding user response:

Your listener is only being added to documents where accepted is already set to 1. Remove .whereEqualTo("accepted", 1) when registering the listener, or change it to 0 to only listen to documents that have not been accepted yet.

  • Related