2

In Firebase addOnSuccessListener method will be called when the app in online mode. But it is not called when offline mode. Following is my code:

mFirebaseDatabase.setValue(newAreaModel)
    .addOnSuccessListener(new OnSuccessListener<Void>()
    {
        @Override
        public void onSuccess(Void aVoid)
        {
            finish();
        }
    })
    .addOnFailureListener(new OnFailureListener()
    {
        @Override
        public void onFailure(@NonNull Exception e)
        {
            Toast.makeText(getApplicationContext(), "Something went Wrong!", Toast.LENGTH_SHORT).show();
        }
    });

Now I want to finish the activity when setValue() method called in offline mode.

Can anyone suggest me how to do that?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ajay dhadhal
  • 306
  • 4
  • 16

2 Answers2

2

When there is a loss of network connectivity (there is no network connection on user's device), neitheronSuccess() nor onFailure() methods are triggered. This has always been the behavior in the Firebase Realtime Database. Only writes that are committed on Firebase servers are considered succeeded or failed, before that they're pending.

Now I want to finish the activity when setValue() method called in offline mode.

Unfortunately, as far as I know, there is no event that triggers when the data has been written to the local queue.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • [WorkPlaceRef. **add** (DATA).addOnCompleteListener({...})..addOnFailureListener(..);] / addOnFailerListner does not trigger when there are no network connectivity. but [WorkPlaceRef . **get** (DATA).addOnCompleteListener({...})..addOnFailureListener(..);] FailureListener working perfectly when there are no network connectivity.Do you know the reason for that? – Isuru Bandara Nov 14 '20 at 15:55
  • @IsuruBandara I'm not sure I understand your question, I recommend you post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 14 '20 at 15:57
  • yes I created a question :) (https://stackoverflow.com/questions/64835933/addonfailurelistener-not-working-in-offline-mode-and-network-issues) – Isuru Bandara Nov 14 '20 at 16:11
1

According to Firebase Guru Frank van Puffelen

There is no event that triggers when the data has been written to the local queue.

https://stackoverflow.com/a/46014750/8518676

Nick Bradshaw
  • 56
  • 1
  • 4