FrangSierra/RxFirebase

ValueEventListener not removed.

macxtor opened this issue · 2 comments

  • If i create a disposable like this and dispose it later in onStop, disposable gets disposed but valueEventListener continues to listen.
private Disposable readNotification;
 readNotification = RxFirebaseDatabase.observeValueEvent(databaseRef), DataSnapshotMapper.listOf(Notification.class))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(notification ->
                        {
                       //something here;
                        }          
                );
readNotification.dispose();

Hello @macxtor , are you sure about this?

The implementation of observeValueEvent removes the EventValueListener when the disposable is dispose:

   /**
     * Listener for changes in te data at the given query location.
     *
     * @param query    reference represents a particular location in your Database and can be used for reading or writing data to that Database location.
     * @param strategy {@link BackpressureStrategy} associated to this {@link Flowable}
     * @return a {@link Flowable} which emits when a value of the database change in the given query.
     */
    @NonNull
    public static Flowable<DataSnapshot> observeValueEvent(@NonNull final Query query,
                                                           @NonNull BackpressureStrategy strategy) {
        return Flowable.create(new FlowableOnSubscribe<DataSnapshot>() {
            @Override
            public void subscribe(final FlowableEmitter<DataSnapshot> emitter) throws Exception {
                final ValueEventListener valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        emitter.onNext(dataSnapshot);
                    }

                    @Override
                    public void onCancelled(final DatabaseError error) {
                        if (!emitter.isCancelled())
                            emitter.onError(new RxFirebaseDataException(error));
                    }
                };
                emitter.setCancellable(new Cancellable() {
                    @Override
                    public void cancel() throws Exception {
                        query.removeEventListener(valueEventListener);
                    }
                });
                query.addValueEventListener(valueEventListener);
            }
        }, strategy);
    }

I have do some tests on local and the listener is properly disposed. If this is not happening to you, could you provide me the real example where you are checking this?

Thank you so much

Hey Frang,

Apologies, it was totally my mistake, was initiating EventListener twice and disposing it once.

Thank you so much for your time.