Firestore - 具有内部Object的对象 [英] Firestore - object with inner Object
问题描述
带有(DocumentSnapShot documentsnapShot)参数的自定义对象。也是firebsae的内部对象,它检索快照并将值设置为我的自定义模型也有其参数(DocumentSnapShot documentsnapShot)。但是,我希望从文件中获取数据并将其传递给我的自定义参数,因为我的不仅仅是firebase还有多个数据。并且不能在没有覆盖的情况下迭代firestore。
A custom object that takes a parameter of (DocumentSnapShot documentsnapShot). also is an inner object from firebsae 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 firabase and pass it to my custom argument because mine takes multiple data not only firebase. And its not possible to iterate firestore without an override.
继承代码
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"));
}
});
}
推荐答案
你现在不能退货尚未加载。 Firestore异步加载数据
,因为这可能需要一些时间。根据您的连接速度和状态,可能需要几百毫秒到几秒才能获得数据。如果要将 settings
对象传递给另一个方法,只需在 onSuccess()
方法中调用该方法并传递该对象作为一个论点。所以快速解决这个问题:
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.
请记住, onSuccess()
方法具有异步行为,这意味着甚至在从数据库获取数据之前就会调用它。如果要在该方法之外使用设置
对象,则需要创建自己的回调
。要实现这一点,首先需要创建一个这样的接口:
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);
}
});
}
最后只需调用 readData()
方法并将 MyCallback
接口的实例作为参数传递给你需要的地方:
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());
}
});
这是你可以使用 UserAccountSettings <的唯一方法/ code> class
onSuccess()
方法。有关更多信息,您还可以查看 视频 。
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.
这篇关于Firestore - 具有内部Object的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!