Java – firestore – objects with internal objects
Custom object with (documentsnapshot documentsnapshot) parameter It is also the internal object of firebsae. It retrieves the snapshot and sets the value to my custom model. It also has its parameter (documentsnapshot documentsnapshot) However, I want to get data from the file and pass it to my custom parameters, because I have more than just firebase And it is impossible to iterate over firestore without coverage
Successor's 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")); } }); }
Solution
You cannot return content that has not been loaded at this time Firestore loads data asynchronously because it may take some time Depending on your connection speed and status, it may take hundreds of milliseconds to seconds to obtain data If you want to pass the set object to another method, you just call the method in the onSuccess () method and pass the object as a parameter. So quickly solve this problem:
@Override public void onSuccess(DocumentSnapshot documentSnapshot) { UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class); yourMethod(settings); }
So remember that the onsuccess () method has asynchronous behavior, which means that it will be called even before getting data from the database If you want to use the setting object outside this method, you need to create your own callback To achieve this, you first need to create such an interface:
public interface MyCallback { void onCallback(UserAccountSettings settings); }
Then you need to create a method that actually gets the data from the database This method should be as follows:
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); } }); }
Finally, just call the ReadData () method and pass the instance of mycallback interface as a parameter to where you need it, as shown below:
readData(new MyCallback() { @Override public void onCallback(UserAccountSettings settings) { Log.d("TAG",settings.getDisplay_name()); } });
This is the only method outside the onsuccess () method that can use an object of the useraccountsettings class For more information, you can also view this video