I want to make static method in helper class to check reference existation in firebase with return type boolean. But "boolean isExist" needs to be final to access nested class "onDataChange" so I can't change its value inside this nested class. How to best solve this problem?
public static boolean checkExist(final Context context, String id) {
final boolean isExist = false;
mEventsDatabaseReference.child(id).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
isExist = true;
} else {
isExist = false;
Toast.makeText(context, "Sorry, this coverage was deleted.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return isExist;
}