在Firebase中设置onDataChange中的变量(singleValue Listener) [英] Setting variable inside onDataChange in Firebase (singleValue Listener)
问题描述
public class FireBaseTest extends .... {
String userName;
$ b mDatabase.child(Users)。child(key).addListenerForSingleValueEvent(
new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//获取用户值
userName = dataSnapshot.child(userName).getValue(String.class);
System.out .println(userName); // Not null
}
$ b $ @Override
public void onCancelled(DatabaseError databaseError){
}
});
...
System.out.println(userName); //空
编辑/更新:
所以之前我有一个进程,正在聆听的价值,并做了一些事情。我只是做了一些调整,并将其添加到onDataChanged方法,以结束我的痛苦。即使它不完全令人满意,它的工作。感谢所有的回答。 解析方案
Firebase中的监听器是异步的,所以你不能像这样设置一个变量。你应该在已经声明你的监听器的函数中传递一个回调函数。
这个回调函数将从firebase调用中得到结果并对其进行进一步的处理。 / b>
public void doSomething(@NotNull final Callback callback){
final Query query = mDatabase.child FirebaseConstants.TARGET);
query.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
String userName = dataSnapshot.child(userName) .getValue(String.class);
callback.OnComplete(userName);
}
$ b $ @Override
public void onCancelled(DatabaseError databaseError){
}
});
}
This is just an example of what my code is like. Right now, if I print out the userName variable inside onDataChange, it works fine. But if I try printing userName outside, after the listener, it will print 'null'. How would I be able to store a variable, with the data I want, inside onDataChange()?
public class FireBaseTest extends .... {
String userName;
...
mDatabase.child("Users").child(key).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
userName = dataSnapshot.child("userName").getValue(String.class);
System.out.println(userName); //Not null
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
...
System.out.println(userName); //Null
EDIT/UPDATE: So before I had a process that would take the value that was being listened and do something with it. I just made some adjustments to that process and added it in the onDataChanged method just to end my suffering. Even though it's not entirely satisfying, it worked. Thank you to all who answered.
Listeners in Firebase are asynchronous, so you can not set a variable like that. You should pass a callback function in the function where you have declared your listener.
This callback function will then take the result from firebase call and do further processing on it.
public void doSomething(@NotNull final Callback callback) {
final Query query = mDatabase.child(FirebaseConstants.TARGET);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userName = dataSnapshot.child("userName").getValue(String.class);
callback.OnComplete(userName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这篇关于在Firebase中设置onDataChange中的变量(singleValue Listener)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!