Check if Firestore query is empty

RonTLV :

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

Alex Mamo :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123770&siteId=1