Android Firebase onDataChange不被调用? [英] Android Firebase onDataChange is not called?
问题描述
onDataChange
方法不会被调用。 Firebase(firebase)(my url); MyCode:
firebase.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.d(TAG,onDataChange:+ dataSnapshot);
if(dataSnapshot.getValue()!= null){
Map map =(Map)dataSnapshot.getValue();
Iterator accountIterator = map.entrySet()。iterator();
while(accountIterator.hasNext()){
Map.Entry accountPair =(Map.Entry)accountIterator.next() ;
$ b $ Log.d(TAG,accountPair:Key:+ accountPair.getKey()+,Value:+ accountPair.getValue());
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError){
Log.d(TAG,onCancelled:+ firebaseError);
}
});
请让我知道。我无法在代码中找到任何问题。请建议我一些解决方案。 解决方案
首先要做的是检查你是否有正确的权限来读取数据库。转到
https://console.firebase.google。 com / project / [your_database_url] / database / rules 并检查。
{
rules:{
.read:true,
.write:auth!= null
}
}
接下来的一个问题是要记住,监听器是异步触发和忘记调用。因此,一旦数据在onDataChange()方法中被接收,对数据的任何操作都应该完成。
I have more than 40,000 nodes in my Firebase Database. But whenever I am trying to read a key from my Android code, the onDataChange
method is not getting called.
MyCode:
Firebase firebase = new Firebase(my url);
firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange : " + dataSnapshot);
if (dataSnapshot.getValue() != null) {
Map map = (Map) dataSnapshot.getValue();
Iterator accountIterator = map.entrySet().iterator();
while (accountIterator.hasNext()) {
Map.Entry accountPair = (Map.Entry) accountIterator.next();
Log.d(TAG, "accountPair : Key : " + accountPair.getKey() + " , Value : " + accountPair.getValue());
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d(TAG, "onCancelled : " + firebaseError);
}
});
I want to retrieve Student node details
Please let me know. I am not able to find any issue in the code. Please suggest me some solution.
First thing to do is to check you have correct permissions to read from your database. Go to https://console.firebase.google.com/project/[your_database_url]/database/rules and check . Not ideal in prod, but for testing you can set read to true
{
"rules": {
".read": true,
".write": "auth != null"
}
}
Next gotcha is to remember that the listeners are asynchronous fire and forget calls. So any manipulations to the data should be done once the data has been received in the onDataChange() method.
这篇关于Android Firebase onDataChange不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!