简体   繁体   中英

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: 在此处输入图像描述

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.

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM