Firebase检索数据Null外部方法 [英] Firebase retrieve data Null outside method
问题描述
我将以下方法放入onCreate()
中,当应用启动时,它将触发此方法.但是,当我尝试将mName
移到ValueEventListener()
之外时,它返回null
并且应用程序崩溃了.
I put the below method inside an onCreate()
, when the app launch it will trigger this method. But when I try to to get mName
outside the ValueEventListener()
, it return null
and the app crashed.
任何人都知道为什么会发生此问题吗?
Anyone know why this issue happen?
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserDetails info = dataSnapshot.getValue(UserDetails.class);
Log.d(TAG, String.valueOf(dataSnapshot));
mName = info.getName();
**Log.d(TAG, mName); // HERE GET THE NAME**
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
};
mDatabase.child("Admin")
.child("Info")
.child(uid)
.addValueEventListener(postListener);
**Log.d(TAG, mName); // HERE GET NULL**
推荐答案
正如其他人所说:Firebase数据库中的所有数据都是异步读取的.通过在代码中添加更多日志语句,最容易看到这种情况:
As the others have said: all data from the Firebase Database is read asynchronously. It's easiest to see this by adding some more log statements to your code:
Log.d(TAG, "Before attaching listener");
mDatabase.child("Admin")
.child("Info")
.child(uid)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "In listener's onDataChange");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
Log.d(TAG, "After attaching listener");
与您期望的不同,它的输出是:
Unlike what you may expect, the output of this is:
在附加侦听器之前
Before attaching listener
附加监听器后
内部侦听器的onDataChange
Inside listener's onDataChange
处理异步行为的方法是重新构造问题.
The way to deal with the asynchronous behavior is to reframe your problem.
现在,您的代码已被写入:首先我们加载数据,然后我们对其进行记录.
Right now your code is written: first we load the data, then we log it.
如果将问题重新组织为:每当数据加载时,我们都会记录",您将获得如下代码:
If you reframe the problem to: "whenever the data is loaded, we log it", you get code like this:
mDatabase.child("Admin")
.child("Info")
.child(uid)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserDetails info = dataSnapshot.getValue(UserDetails.class);
Log.d(TAG, String.valueOf(dataSnapshot));
mName = info.getName();
Log.d(TAG, mName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
因此,所有需要数据的代码都必须位于onDataChange()
内部.或者,您可以创建自己的自定义回调方法,然后从onDataChange()
调用该方法.但是逻辑总是相同的:需要数据的代码是在数据可用时从onDataChange()
触发的.
So all code that needs the data, needs to be inside the onDataChange()
. Alternatively, you can make your own custom callback method and invoke that from onDataChange()
. But the logic is always the same: the code that needs toe data, is triggered from onDataChange()
when the data is available.
这篇关于Firebase检索数据Null外部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!