0

I am working on developing one Android application and I have decided that I want to save the data in Firebase. Currently I have one obstacle with loading the data. First off all I would like to show the architecture of application:

Architecture of application

In method OnCreate is created the instance of FirebaseHandler (it is singleton class). After the instance exist, MainActivity call method nameOfMember.

 public void nameOfMember() {
    DocumentReference documentReference = db.document(pathTOOffersPrefix + userID);
    Log.d(TAG, "nameOfMember: " + pathTOOffersPrefix + userID);

    documentReference.get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        String name = documentSnapshot.getString(ARG_Firebase_User_Name);
                        String surname = documentSnapshot.getString(ARG_Firebase_User_SurName);
                        setName(name);
                        setSurname(name);
                        Log.d(TAG, "onSuccess: " + name + " " + surname);
                    } else {
                        Log.d(TAG, "onSuccess: but not exist");
                    }

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "onFailure: " + e.getMessage());
                }
            });
}

This method setUp two argument of FirebaseHandler instance (for future using). It is working but now come the problem. In Main.java I try to get this argument:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firebaseHandler = FirebaseHandler.getInstance();
        firebaseHandler.getName();
        firebaseHandler.getSurname();
}

The Main problem is, that the method Main.onCreate will be finisihed earlier than FirebaseHandler.onSuccess.

I would like to ask, if anybody know how to solve it (some callback? or).

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Marek Vondra
  • 59
  • 1
  • 9

1 Answers1

1

Yes this is normal behavior of background processing since the Firebase Database functions are executed inside a separate thread, then you need to do the following:

  1. Create an interface to be a listener for the getString() method:

    public interface OnValueReadyListener {
        void onValueReady(String name, String surName);
    }
    
  2. Pass an instance of the interface to the nameOfMember method:

    public void nameOfMember(OnValueReadyListener listener) {
        ...
    }
    
  3. Inside the onSuccess method, call the listener:

    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists()) {
            String name = documentSnapshot.getString(ARG_Firebase_User_Name);
            String surname = documentSnapshot.getString(ARG_Firebase_User_SurName);
            listener.onValueReady(name, surname);
            Log.d(TAG, "onSuccess: " + name + " " + surname);
        } else {
            Log.d(TAG, "onSuccess: but not exist");
        }
    
    }
    
  4. Call the method nameOfMember from the onCreate as following:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firebaseHandler = FirebaseHandler.getInstance();
        firebaseHandler.nameOfMember(new OnValueReadyListener() {
             public void onValueReady(String name, String surName) {
                 // Do what you need here. you have the values ready
             }
        });
    
    }
    
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43