Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase offline no CompletionListener on setValue

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.

like image 778
Arka Avatar asked May 12 '16 10:05

Arka


People also ask

Is it possible to use firebase offline?

The Firebase Realtime Database stores data returned from a query for use when offline. For queries constructed while offline, the Firebase Realtime Database continues to work for previously loaded data. If the requested data hasn't loaded, the Firebase Realtime Database loads data from the local cache.

Is firebase a database?

The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.


1 Answers

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.

like image 118
Frank van Puffelen Avatar answered Oct 13 '22 00:10

Frank van Puffelen