Reputation: 1837
I want to set a listener in my firestore database.
I want to listen to a value called isConfirmClicked
and in case it equals 1 I want to know about it.
I use the following:
db.collection( "Users" ).document( NotMyID ).collection( "MyChats" ).whereEqualTo( "isConfirmClicked" , 1 ).addSnapshotListener( new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
DO SOMETHING HERE
}
} );
For some reason, it always enters to the DO SOMETHING HERE
even when for sure isConfirmClicked equals to 0.
Is there any reason for this?
Upvotes: 0
Views: 133
Reputation: 317427
The listener you provide will be invoked whenever the results are known from the query, even if there are no matching documents. If you want to know if there are matching documents for your query, you will need to check the provided QuerySnapshot using its isEmpty() method.
@Override
public void onEvent(@Nullable QuerySnapshot querySnapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
if (!querySnapshot.isEmpty()) {
// here, there is at least one document that matches the query
}
}
Or, you can check if querySnapshot.size()
> 0.
Upvotes: 1