Remove Firestore Snapshot Listener From Inside Listener

Viewed 1137

Super simple question. I need to remove a firestore snapshot listener once I hit a certain point inside of my listener. Seems like this should be super easy but I can't figure it out. Thanks!

Example code:

val registration = gameRef.addSnapshotListener { snapshot, e ->
     //code....

     //if(condition is meet){
     //    destroy this listener
     //}
}
1 Answers

Assume that the listener is returned immediately and that its callback won't be invoked until after it's returned by the function:

var feedback: ListenerRegistration? = null
feedback = gameRef.addSnapshotListener { snapshot, e ->
    feedback?.remove()
}
Related