How to return a DocumentSnapShot as a result of a method?

A custom object that takes a parameter of (DocumentSnapShot documentsnapShot). also is an inner object from Firebase that retrieves a snapshot and set the values to my custom model also have its argument (DocumentSnapShot documentsnapShot). However, I wish to get the data from Firebase and pass it to my custom argument because mine takes multiple data not only Firebase. And it’s not possible to iterate Firestore without an override.

Here’s the code:

public UserSettings getUserSettings(DocumentSnapshot documentSnapshot){     Log.d(TAG, "getUserSettings: retrieving user account settings from firestore");      DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);     mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {         @Override         public void onSuccess(DocumentSnapshot documentSnapshot) {             UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);             settings.setDisplay_name(documentSnapshot.getString("display_name"));             settings.setUsername(documentSnapshot.getString("username"));             settings.setWebsite(documentSnapshot.getString("website"));             settings.setProfile_photo(documentSnapshot.getString("profile_photo"));             settings.setPosts(documentSnapshot.getLong("posts"));             settings.setFollowers(documentSnapshot.getLong("followers"));             settings.setFollowing(documentSnapshot.getLong("following"));         }     }); } 
Add Comment
2 Answer(s)

You cannot return something now that hasn’t been loaded yet. Firestore loads data asynchronously, since it may take some time for this. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before that data is available. If you want to pass settings object to another method, just call that method inside onSuccess() method and pass that object as an argument. So a quick fix would be this:

@Override public void onSuccess(DocumentSnapshot documentSnapshot) {     UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);     yourMethod(settings); } 

One more thing to mention is that you don’t need to set the those values to object that already have them. You are already getting the data from the database as an object.

So remember, onSuccess() method has an asynchronous behaviour, which means that is called even before you are getting the data from your database. If you want to use the settings object outside that method, you need to create your own callback. To achieve this, first you need to create an interface like this:

public interface MyCallback {     void onCallback(UserAccountSettings settings); } 

Then you need to create a method that is actually getting the data from the database. This method should look like this:

public void readData(MyCallback myCallback) {     DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);     mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {         @Override         public void onSuccess(DocumentSnapshot documentSnapshot) {             UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);             myCallback.onCallback(settings);          }     }); } 

In the end just simply call readData() method and pass an instance of the MyCallback interface as an argument wherever you need it like this:

readData(new MyCallback() {     @Override     public void onCallback(UserAccountSettings settings) {         Log.d("TAG", settings.getDisplay_name());     } }); 

This is the only way in which you can use that object of UserAccountSettings class outside onSuccess() method. For more informations, you can take also a look at this video.

Answered on August 29, 2020.
Add Comment

Use LiveData as return type and observe the changes of it’s value to execute desired operation.

private MutableLiveData<UserAccountSettings> userSettingsMutableLiveData = new MutableLiveData<>();  public MutableLiveData<UserAccountSettings> getUserSettings(DocumentSnapshot documentSnapshot){      DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);     mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {         @Override         public void onSuccess(DocumentSnapshot documentSnapshot) {             UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);             settings.setDisplay_name(documentSnapshot.getString("display_name"));             settings.setUsername(documentSnapshot.getString("username"));             settings.setWebsite(documentSnapshot.getString("website"));             settings.setProfile_photo(documentSnapshot.getString("profile_photo"));             settings.setPosts(documentSnapshot.getLong("posts"));             settings.setFollowers(documentSnapshot.getLong("followers"));             settings.setFollowing(documentSnapshot.getLong("following"));              userSettingsMutableLiveData.setValue(settings);         }     });      return userSettingsMutableLiveData; } 

Then from your Activity/Fragment observe the LiveData and inside onChanged do your desired operation.

getUserSettings().observe(this, new Observer<UserAccountSettings>() {     @Override     public void onChanged(UserAccountSettings userAccountSettings) {         //here, do whatever you want on `userAccountSettings`     } }); 
Answered on August 29, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.