2

I'm adding a listener to a task returned from a Firestore get() request:

myQuery.get().addOnSuccessListener(Runnable::run) 
    { 
       /*does something*/ 
    }

I know I can scope the listener to an activity with addOnSuccessListener(activity, ...) which would remove the listener when the activity stops. But, to keep my code clean I need to remove the listener myself (when the rxJava observable that wraps the request is disposed of - using 

emitter.setCancellable{ 
/*remove the listener here*/ 
}

).

How can I remove the listener from the task?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Tom Gatward
  • 129
  • 3

1 Answers1

2

Instead of using a Task listener, try adding a snapshot listener on the Query itself.

This method returns a ListenerRegistration object that has a remove method.

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • I guess i could add a snapshot listener and call remove on the ListenerRegistration after the first update as well as when it needs disposing. That seems dirty though and i'm confused as to why the same can't be done for a simple get request. – Tom Gatward Aug 24 '18 at 11:25