How to Break For each in Android?
FirebaseFirestore.getInstance().collection("registration").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
for(DocumentSnapshot d:value){
if(d.getId().equals(Name)){
Toast.makeText(MainActivity.this, "User Name already exists", Toast.LENGTH_SHORT).show();
break;
}
else{
is_userExists = false;
}
}
}
});
I want to break the foreach loop but the code isnt working properly
Use return keyword instead of break;
FirebaseFirestore.getInstance().collection("registration").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
for(DocumentSnapshot d:value){
if(d.getId().equals(Name)){
Toast.makeText(MainActivity.this, "User Name already exists", Toast.LENGTH_SHORT).show();
return;
}
else{
is_userExists = false;
}
}
}
});
It may be more logical to do this operation with a void method and return instead of break.