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")); } }); }
A QueryDocumentSnapshot contains data read from a document in your Cloud Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted using the getData() or the various get() methods in DocumentSnapshot (such as get(String) ).
A DocumentSnapshot contains data read from a document in your Cloud Firestore database. The data can be extracted with the getData() or get(String) methods. If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null .
A QuerySnapshot contains zero or more DocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property or enumerated using the forEach method. The number of documents can be determined via the empty and size properties.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With