RonTLV
RonTLV

Reputation: 2556

Check if Firestore query is empty

I'm running a query to check on a boolean (isLocked), to know if one or more of the documents are locked:

final CollectionReference ref = FirebaseFirestore.getInstance().collection( "folders" ).document( user.getUid() ).collection( folder );

Query query = ref.whereEqualTo( "isLocked", true );
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        boolean b = task.getResult().isEmpty();
        ToastEX.showShort( MainActivity.this, b ? "isLocked=1" : "isLocked=0");
    }
});

This always return isLocked=1, regardless of what's on the database.

How do I get this to work? Thanks a lot.

EDIT: Adding screenshot: enter image description here

Upvotes: 4

Views: 3510

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

When you override the onComplete() method, always make sure to check if the task is successful like in the following lines of code:

if (task.isSuccessful()) {
    boolean b = task.getResult().isEmpty();
    ToastEX.showShort( MainActivity.this, b ? "isLocked=1" : "isLocked=0");
}  else {
    Log.d(TAG, "Error getting documents: ", task.getException());
}

Try aslo not to forget to implement the else part to see if you have an error message.

Upvotes: 2

Related Questions