Reputation: 37
Following the previous thread that I have created here: How do I set a variable inside of an onComplete method?
I am still trying to call the variable that I have set inside on the onComplete
method. I know there is a delay in-between accessing the method. And the one solution that was brought up to me was to create an interface. Although, I have successfully implemented the interface, I am not sure whether my implementation is correct since I am still getting nulls.
Here's my Interface:
public interface MyCallBack {
void onCallFullName(String fullName);
void onCallEmail(String Email);
}
The method to fetch the data from firebase:
private void callUserCredential(MyCallBack myCallBack) {
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
String userID = fAuth.getCurrentUser().getUid();
DocumentReference docRef = fStore
.collection("users")
.document(userID);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if(document.exists()){
//Log for successfully fetching data from firebase
Log.d(TAG,"User full name: " + document.getString("FullName"));
Log.d(TAG, "User email: " + document.getString("Email"));
String fullName = document.getString("FullName");
String eMail = document.getString("Email");
//Log for successfully setting the data into the variables
Log.d(TAG, "donorFullName set: " + fullName);
Log.d(TAG, "donorEmail set: " + eMail);
//Interface callback
myCallBack.onCallFullName(fullName);
myCallBack.onCallEmail(eMail);
}
}
}
});
}
and Lastly, here's me calling the Interface:
//Create booking information
BookingInformation bookingInformation = new BookingInformation();
callUserCredential(new MyCallBack() {
@Override
public void onCallFullName(String fullName) {
donorFullName = fullName;
}
@Override
public void onCallEmail(String Email) {
donorEmail = Email;
}
});
//Log for successfully calling the interface
Log.d(TAG, "donorFullName bookingInformation: " + donorFullName);
Log.d(TAG, "donorEmail bookingInformation: " + donorEmail);
bookingInformation.setDonorName(donorFullName);
bookingInformation.setDonorEmail(donorEmail);
The logcat still shows the same thing:
Upvotes: 0
Views: 137
Reputation: 84
You must setDonorName() and setDonorEmail() in Callback functions like:
//Create booking information
BookingInformation bookingInformation = new BookingInformation();
callUserCredential(new MyCallBack() {
@Override
public void onCallFullName(String fullName) {
bookingInformation.setDonorName(fullName);
//Log for successfully calling the interface
Log.d(TAG, "donorFullName bookingInformation: " + fullName);
}
@Override
public void onCallEmail(String email) {
bookingInformation.setDonorEmail(email);
//Log for successfully calling the interface
Log.d(TAG, "donorEmail bookingInformation: " + email);
}
});
Because when you call the log function, the request is uncompleted yet!
Upvotes: 2