0

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;
}
Panicum
  • 794
  • 1
  • 9
  • 32
  • 1
    You cannot return something now that hasn't been loaded yet. So please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Apr 11 '19 at 12:23
  • 1
    Make your final isExist a TextView type, then do isExist.setText(true)) or false based on the condition, then later get it with textview.getText().toString() – Mocas Apr 11 '19 at 12:31

0 Answers0