0

I am currently testing the new firebase on how efficiently we can work with the database offline. For database online offline state we have .info/connected path to check if database is currently connected or not. And I can change the state with the following code:

mDbStatusRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                Boolean connected = snapshot.getValue(Boolean.class);
                mDbStatus.setText(String.valueOf(connected).toUpperCase());
                if (!connected)
                    mDbStatus.setTextColor(getResources().getColor(android.R.color.holo_red_light));
                else
                    mDbStatus.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
            }

            @Override
            public void onCancelled(DatabaseError error) {
                System.err.println("Listener was cancelled");
            }
        });

Is there any way I can be sure that the database on the local client (cache is what you call) is fully synced or not? Like are there any pending write queues in the cache for the remote?

Or any alternatives/hacks through which I can be sure everything is synced and It is safe to close the connection. It will be useful to sync everytime app starts and close the connection once everything is synced. Or keep the the connection open when there is a need for the realtime syncing and close it once the user is done with it and everything is synced up with the server. And many other use-cases like this.

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • There are currently no API methods (or values in the JSON tree) that you can monitor to determine the synchronization state. Agreed that it would be very useful to have those btw, but they don't exist at the moment. – Frank van Puffelen May 28 '16 at 20:52
  • See this answer from a while back, which (at first glance) seems still accurate: http://stackoverflow.com/questions/35280502/firebase-synchronisation-of-locally-modified-data-handling-errors-global-stat – Frank van Puffelen May 28 '16 at 20:52
  • Thanks @FrankvanPuffelen for the info. Can you explain or provide an example of the para from your linked answer: `That depends on the use-case. But if you want to simply know when all your writes are executed, push a dummy value and wait for that to complete. Since Firebase executes the writes in order, you can be certain at that stage that you've gotten the other events.` – kirtan403 May 29 '16 at 04:35
  • In JavaScript syntax (shorter): `var marker = ref.push(); marker.set('marker', function() { /* done writing marker, so everything before it myst also be written */ marker.remove(); })` – Frank van Puffelen May 29 '16 at 15:06
  • Ohh yes ! This can work. But can be problemetic with the code like, i am writing 10 records to db on button click and then this `marker`. But if user clicks on button multiple times quickly, then this would fail. – kirtan403 May 29 '16 at 15:10

0 Answers0