You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the failing case, this references two different objects, and thus this._handles.has(callback) resolves to false and the listener is not removed.
This can be resolved by creating the reference first, then using that same reference for both the on and off invocations, as shown in the success example above.
The second scenario is when a common callback (event handler) is used. In the off method above, the handle, event type, and context all are deleted from the callback when the listener is removed. If you use that same callback function on a subsequent off call, the handle will resolve to undefined and the block that removes the listener will be skipped.
This can be resolved by invoking the common callback within a unique outer function, such as
I recommend revising the code example in the database readme topic Remove-a-reference-event-listener, and adding a note that a unique callback function must be used with each on invocation.
The text was updated successfully, but these errors were encountered:
I have encountered two scenarios that can cause database listeners (such as 'child_changed') to fail to remove.
Here's the code at the end of the
on
method :this
in this case is the path reference, such asfirebase().database().ref('user/data')
.Consequently, this code will work correctly:
Whereas this code will fail to remove the listener:
Because the
off
method references the handle saved by theon
method, but above you have a different instance of the reference.Here's the entirety of the
off
method:In the failing case,
this
references two different objects, and thusthis._handles.has(callback)
resolves to false and the listener is not removed.This can be resolved by creating the reference first, then using that same reference for both the
on
andoff
invocations, as shown in the success example above.The second scenario is when a common callback (event handler) is used. In the
off
method above, the handle, event type, and context all are deleted from the callback when the listener is removed. If you use that same callback function on a subsequentoff
call, the handle will resolve toundefined
and the block that removes the listener will be skipped.This can be resolved by invoking the common callback within a unique outer function, such as
I recommend revising the code example in the database readme topic Remove-a-reference-event-listener, and adding a note that a unique callback function must be used with each
on
invocation.The text was updated successfully, but these errors were encountered: