问题
addOnFailureListner does not work for add() data, but addOnFailureListner works for get().
This is not working
WorkPlaceRef.add(DATA).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
//Successfully created - This one triggers when I turn on wifi again.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error - This addonFailureListner is not working when there are no network.
}
});
This is working
WorkPlaceRef.get().addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
//Successfully received - This one triggers when I turn on wifi again.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error - this addonFialureListner triggers when there is no network.
}
});
回答1:
It's not a failure to attempt to write data while offline. The Firestore SDK will write the data in a local offline cache first, and eventually synchronize that write with the server when the app comes back online. The success listener will be invoked whenever that happens.
Write failures only happen when there is some problem that can't be retried due to lack of connectivity, such as security rule violations, or exceeding some documented limit of the database.
If you want to know if some document data is not yet synchronized with the server, you can check its metadata to know if a write is pending.
回答2:
The get functionality is working because the call could not take place, whereas in add it must be getting completed that's why it is not showing in OnFailure. Also read below.
public abstract Task<TResult> addOnFailureListener (OnFailureListener listener)
Adds a listener that is called if the Task fails.
The listener will be called on main application thread. If the Task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.
A canceled Task is not a failure Task. This listener will not trigger if the Task is canceled. For more information visit this link.
来源:https://stackoverflow.com/questions/64835933/addonfailurelistener-not-working-in-offline-mode-and-network-issues