6

I have this codebase where I set a value. In offline mode it writes successfully but is not calling the CompletionListener.onComplete callback function.

newOrderRef.setValue(order, (firebaseError, firebase) -> {
            if (firebaseError != null) {
                Timber.e(firebaseError.toException(), "Order create failed, id: %s", order.getOrderId());
                subscriber.onError(firebaseError.toException());

            } else {
                Timber.i("Order created, id: %s", order.getOrderId());
                newOrderRef.setPriority(0 - timestamp);
                subscriber.onNext(firebase.getKey());
                subscriber.onCompleted();
            }
        });

The callback never gets called. But writes fine.

In another case even after unsubscribing in onDestroy using CompositeSubscription, the subscriber gets called when the value gets to write to firebase server even when the fragment is not running.

Is this the correct behavior ?

      Subscription orderSubscription = OrderManager.createOrder(order)
                        .subscribe(s -> {
                            fabShowSuccess();
                            showSnackbar("onnext Order created " + order.getOrderId());
                        }, throwable -> {
                            showSnackbar("Order failed. Make sure your are connected to internet.");
                            fabShowFailed();
                        }, () -> {
                            fabShowSuccess();
                            showSnackbar("Order created " + order.getOrderId());
                        });
                mCompositeSubscription.add(orderSubscription);

In onDestroy() I call mCompositeSubscription.unsubscribe(); , but the subscriber gets called later.

Arka
  • 1,073
  • 1
  • 10
  • 14

1 Answers1

4

The completion listener for write operations will be invoked when the write has been committed to the database on the Firebase servers. When you're offline, that won't happen.

There is no way to unsubscribe a completion listener. If the listener still exists when the write operation completes, it will be invoked.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Thank you for your answer. I got around the issue by checking if network is available then returning error. – Arka May 12 '16 at 14:54
  • 3
    So is there any other way to be notified when data was successfully written to the local database? After all, it is an IO operation which could take a while. I was thinking of setting a value listener on the same location as I am about to write to, and wait for the onChanged callback (which is called properly). However this approach doesn't seem too safe as I would probably have to check whether the new value is the same as the value i am trying to write. Thanks for feedback! – Kuba Spatny Dec 20 '16 at 07:30
  • @KubaSpatny I also have same doubt, is there any way to check whether a value has been written to the local firebase database? – Sreekanth Karumanaghat Jan 12 '18 at 02:09
  • I set my listener to NULL, but the callback of the CompletionListener is still called. How that is possible? :) Is there some new method for unsubscribe? – MrVasilev Oct 31 '18 at 09:03