[英]onDataChange() in Firebase getting triggered multiple times
在 firebase 中, onDataChange() function 被多次調用,經過一番搜索,我發現它被稱為監聽器連接的次數。 但是我已經多次附加了監聽器,因為我需要不同的查詢並獲取不同類型的數據庫。 我怎樣才能解決這個問題。 我已經使用 addValueEventListener 9 次來檢索或排序存儲在數據庫中不同節點的不同數據。 獲取數據的正確方法是什么。 任何幫助,將不勝感激。 這是我使用過代碼的一些地方。
firebaseUrl.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String temp, temp1;
Log.e(TAG, "onDataChange: mytime " + dataSnapshot.getValue());
long timeStampLong = (long) dataSnapshot.child("time").child("timestamp").getValue();
SimpleDateFormat sfd = new SimpleDateFormat("HH:mm:ss");
temp1 = sfd.format(new Date(timeStampLong));
try {
if (isTimeBetweenTwoTime("08:00:00", "20:00:00", temp1)) {
//do something
}
} catch (ParseException e) {
e.printStackTrace();
}
}
public void onCancelled(DatabaseError databaseError) {
}
});
firebaseUrl.child("time").child("timestamp").setValue(ServerValue.TIMESTAMP);
.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String temp1;
long timeStampLong = (long) dataSnapshot.child("time").child("timestamp").getValue();
SimpleDateFormat sfd1 = new SimpleDateFormat("HH:mm:ss");
temp1 = sfd1.format(new Date(timeStampLong));
onClickShowAnswerButton(temp1);
}
public void onCancelled(DatabaseError databaseError) {
}
});
.
public void onClickShowAnswerButton(String time) {
try {
if (isTimeBetweenTwoTime("08:00:00", "20:00:00", time)) {
showDialogBox("Today's Answer", "The answer can be seen in between 8 PM to 8 AM only");
} else {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String correctAnswerString, yourAnswerString;
String title;
correctAnswerString = dataSnapshot.child("questions").child(imagename).child("correctAnswer").getValue(String.class);
yourAnswerString = dataSnapshot.child("users").child(uid).child("questions").child(imagename).child("yourAnswer").getValue(String.class);
if (correctAnswerString.equals(yourAnswerString))
title = "CorrectAnswer: " + correctAnswerString + "\nCongratulations your answer answer is correct ";
else if (yourAnswerString == null)
title = "Answer: " + correctAnswerString + "\nYou haven't answered the question";
else
title = "Answer: " + correctAnswerString + "\nYour Answer: " + yourAnswerString + "\nSorry your answer is incorrect";
showDialogBox("Today's Answer", title);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
} catch (ParseException e) {
e.printStackTrace();
}
}
IMO您應該做的是一次附加偵聽器,然后通過緩存或持久性存儲將該數據提供給其他方法使用。 您可以創建在更新Firebase時更新的數據對象,並可能使用諸如數據綁定之類的方法來更新UI。 但是,盡管有許多其他地方需要數據,但您僅需要附加一個偵聽器。 擁有數據后,您始終可以獲取數據。
使用 SingleEventListener 而不是 ValueEventListener
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.