Siddharth Gharge Siddharth Gharge - 3 months ago 18
Android Question

Firebase Database Cashed data

Is there any way to know data which is not uploaded on firebase and is persisting on disk? I have enabled offline capability by using-

FirebaseDatabase.getInstance().setPersistenceEnabled(true);


By doing this my all data remains cached. I want to know which data is uploaded and which isnt.

Answer

There is no method in the API that returns an indication of which write operations have not yet been committed to the Firebase server. If you need that status, you can manage it yourself. The setValue() and updateChildren() methods provide two options for getting a callback when the value is committed to the Firebase server. One option is to provide a CompletionListener parameter:

FirebaseDatabase.getInstance().getReference("test")
    .setValue("SomeValue", new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if (databaseError == null) {
                // the data was successfully committed to the server
            } else {
                // the operation failed, for example permission failure
            }
        }
    });

The other option is to add a listener to the returned Task:

FirebaseDatabase.getInstance().getReference("test")
        .setValue("SomeValue").addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            // the data was successfully committed to the server
        } else {
            // the operation failed, for example permission failure
        }
    }
});

For both cases, the update is in the local cache immediately after the call to setValue() or updateChildren(), but is not in the server database until the callback fires.

Comments