0

So here are two situations when there is no network:

Withtout using any query I'm getting an error:

collectionReference.document().get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "onComplete: success");
        } else {
            Log.d(TAG, "onComplete: " + task.getException());
        }
    }
});

The same goes for addOnFailureListener where I'm able to log the errors for the above example i.e.:

Failed to get document because the client is offline

Now, while using queries, I get no error, examples:

collectionReference.limit(1).get().addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.d(TAG, "onFailure: " + e.getMessage());
    }
}).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        Log.d(TAG, "onSuccess: ");
    }
});

Used the limit option. Other case examples:

collectionReference.addSnapshotListener((queryDocumentSnapshots, e) -> {
    if (e != null) {
        Log.d(TAG, "onEvent: error"  + e);
        return;
    }

    if (queryDocumentSnapshots != null) {
        Log.d(TAG, "onEvent: success");
    } else {
        Log.d(TAG, "onEvent: no result");
    }
});

Or using document:

collectionReference.document("path").addSnapshotListener((documentSnapshot, e) -> {
    if (e != null) {
        Log.d(TAG, "onEvent: error"  + e);
        return;
    }

    if (documentSnapshot != null) {
        Log.d(TAG, "onEvent: success");
    } else {
        Log.d(TAG, "onEvent: no result");
    }
});

Another one that returns success:

collectionReference.limit(1).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "onComplete: success");
        } else {
            Log.d(TAG, "onComplete: error " + task.getException());
        }
    }
});

Current dependency:

implementation 'com.google.firebase:firebase-firestore:17.1.0'

All the above examples returns success even if it fails. So is this is a bug or that it's suppose to work like this?

Rajarshi
  • 2,419
  • 3
  • 23
  • 36
  • Can you post the errors? – James Poag Aug 22 '18 at 11:47
  • Check **[this](https://stackoverflow.com/questions/51955413/android-how-to-handle-firestore-exceptions-at-user-level/51965256#51965256)** out and please responde with @. – Alex Mamo Aug 22 '18 at 11:49
  • Edited the post just now. However, for the top snippet, i.e. without query, the error I get is `Failed to get document because the client is offline` but for other ones, it all logs `success`. – Rajarshi Aug 22 '18 at 11:50
  • @Rajarshi Have you checked my answer from that post? Does it answer your question? – Alex Mamo Aug 22 '18 at 12:02
  • @AlexMamo I was doing a test run on what you told on provided link and sadly got both success and error messages, this makes it harder to debug. Why there is no any way to get the network status? **By the way**, what is `task.isComplete()` used for? – Rajarshi Aug 22 '18 at 12:10
  • @Rajarshi Let's stick to one question at a time. You say you get "both success and error messages" but in which case? How does your code look like? That are the exact errors that you get? – Alex Mamo Aug 22 '18 at 12:14
  • @AlexMamo edited the question, see the last snippet, it suppose to do so as you explained – Rajarshi Aug 22 '18 at 12:18
  • Using the last snippet, you cannot get both. You can either get `"onComplete: success"` or `"onComplete: error " + task.getException()`. Maybe you get that error from another part of your code. – Alex Mamo Aug 22 '18 at 12:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178530/discussion-between-rajarshi-and-alex-mamo). – Rajarshi Aug 22 '18 at 13:06

0 Answers0