1

I am getting the error in my onPause() method saying:

Cannot resolve method 'removeEventListener(com.google.firebase.database.ValueEventListener)'

I guess I might have done some mistakes while trying to convert this tutorial (where the app displays whether the receiver of the message has seen the message or not) from Firebase Realtime Database to Firebase Firestore.

Declaration of the ValueEventListener takes place before the onCreate() method

ValueEventListener seenListener;

seenMessage() method

private void seenMessage(final String recipientId){
        final CollectionReference reference = FirebaseFirestore.getInstance().collection("chats");
        seenListener = (ValueEventListener) reference.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(documentSnapshots!=null){
                    for(QueryDocumentSnapshot queryDocumentSnapshots : documentSnapshots){
                        Chat chat = queryDocumentSnapshots.toObject(Chat.class);
                        if (chat.getReceiver().equals(userId) && chat.getSender().equals(recipientId)){
                            HashMap<String, Object> hashMap = new HashMap<>();
                            hashMap.put("isseen", true);
                            reference.add(hashMap);
                        }
                    }
                }
            }
        });
    }

onPause() method

@Override
    protected void onPause() {
        super.onPause();
        FirebaseFirestore.getInstance().collection("chats").removeEventListener(seenListener);
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

1

Is removeEventListener() is not a part of Firebase Firestore?

No, it's not. The following line of code:

reference.addSnapshotListener(new EventListener<QuerySnapshot>() {/* ... */}

Return a ListenerRegistration object and there is no way in Java in which you can cast this type of object to a ValueEventListener object. The ValueEventListener is a part of Firebase Realtime Database and Cloud Firestore is a totally different product.

As I see in your code, you are using Cloud Firestore. If you want to remove the listener, please see my answer from the following post:

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

You're declaring eventListener inside a method:

private void seenMessage(final String recipientId){
    EventListener<DocumentSnapshot> eventListener = new EventListener<DocumentSnapshot>() { ... }
    ...
}

This means that the eventListner variable will only be visible to other code inside that method. You're trying to access it eventListener outside of that method, which java does not allow. This is called "variable scoping". eventListener is "scoped" to the method seenMethod, which is the only place it can be referenced.

You can instead declare and assign eventListener outside of the method as a member variable of the class. Then, you can use it in any method, just like listenerRegistration.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

This is how you can detach an event listener in firestore

  void detachListener() {
   
      Query query = db.collection("cities");
      ListenerRegistration registration = query.addSnapshotListener(
        new EventListener<QuerySnapshot>() {
           @Override
           public void onEvent(@Nullable QuerySnapshot snapshots,
                          @Nullable FirestoreException e) {

            } 
        });
      // ...
      // Stop listening to changes
      registration.remove();
  }
Moumen Lahmidi
  • 462
  • 5
  • 7