artooras
artooras

Reputation: 6795

Firebase Firestore detach a listener doesn't work

I (think I) am following the instructions on how to detach a listener in Firebase Firestore, but in my code the listener remains active.

// attaching a listener
this.dbTasks.where('completed', '==', true).onSnapshot(data => {...code...})

// detaching a listener
this.dbTasks.where('completed', '==', true).onSnapshot(() => {})

What am I missing?

Upvotes: 2

Views: 1737

Answers (1)

Scarygami
Scarygami

Reputation: 15549

onSnapshot returns a function that you need to save in a variable and call when you want to remove the listener.

this.unsubscribe = this.dbTasks.where('completed', '==', true).onSnapshot(
  data => {...code...}
);


// Stop listening to changes
this.unsubscribe();

What you are doing is actually creating a second listener that doesn't do anything.

Upvotes: 9

Related Questions