過去幾周我一直在研究Android應用程序,並且我一直使用Firebase作爲後端。我從firebase讀取/寫入沒有問題,但最近,所有addListenerForSingleValueEvent調用都沒有被觸發。我不確定爲什麼它會突然停止閱讀。我的數據庫規則(用於測試目的)Android Firebase addListenerForSingleValueEvent未調用
"rules": {
".read": true,
".write": true
}
和相關代碼
final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
//searches database for user
//This "LISTENING" println triggers
System.out.println("LISTENING");
mDatabase.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This print statement never prints
System.out.println("DATA SNAPSHOT");
for(DataSnapshot user : dataSnapshot.getChildren()){
for(DataSnapshot participant: user.getChildren()) {
//if we have found the participant name
if(participant.getKey().equalsIgnoreCase("Participant Name")){
System.out.println(participant.getValue().toString().trim());
}
}
}
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Error");
}
});
我也有其他幾個addListenerForSingleValueEvent調用(即在結構上相似,所以爲了簡潔起見,我不會交他們在這裏),工作很好,直到大約2小時前。可能會發生什麼?這可能是Firebase本身的問題嗎?我的代碼中有什麼東西可以忽略?
已解決 - 某種。必須使用不同的模擬器。不清楚問題是什麼。 – JohnDoe
go for addValueEventListener(...) – MrCurious