[英]Firebase increment double do infinite loop
我正在創建一個執行工作的方法,每次onComplete
完成時它都會增加一個值,但是這會導致該字段發生無限循環。
例如,當onComplete
完成時,該方法將執行並必須增加100點,但它會占用這些點並不斷增加,而不會結束。
這是我的方法:
@Override
public void afterTextChanged(Editable editable) {
refTrans.child(finalCurrentUserKey).child(Constants.FIRE_BASE_USER_DRIVER_PLUS).child("birthday").setValue(
etBirthday.getText().toString(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
addPoints(finalCurrentUserKey);
}
});
}
private void addPoints(final String finalCurrentUserKey){
// Create a reference to the DB
final FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
final DatabaseReference refPoints = mDatabase.getReference(Constants.FIRE_BASE_USER_DRIVER);
final DatabaseReference scoreRef = refPoints.child(finalCurrentUserKey).child("score/increaseScore");
//final Double[] count = new Double[1];
final String operation = "increaseScore";
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} /*else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}*/
//mutableData.setValue(1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
refPoints.child(finalCurrentUserKey).child("plusPoints").setValue(count[0]);
// when this statement execute onDataChange method will be called every time
you should use
refPoints.child(finalCurrentUserKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// do something
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// this is going to call onDataChange method once if the value is changed or not
你應該檢查這個帖子
@Alex Mamo感謝您的幫助,我按照您的方式解決了我的問題,並進行了一些驗證,這是我的方法,因此有一天可以為任何人提供幫助
if (TextUtils.isEmpty(etBirthday.getText().toString())) {
obtenerFechaApartir(actvty, etBirthday);
addPoints(finalCurrentUserKey);
etBirthday.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
refTrans.child(finalCurrentUserKey).child(Constants.FIRE_BASE_USER_DRIVER_PLUS).child("birthday").setValue(
etBirthday.getText().toString(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
}
});
}
});
} else {
obtenerFechaApartir(actvty, etBirthday);
etBirthday.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
refTrans.child(finalCurrentUserKey).child(Constants.FIRE_BASE_USER_DRIVER_PLUS).child("birthday").setValue(
etBirthday.getText().toString(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
}
});
}
});
}
好像經過了文本的修改后就形成了一個氣泡,所以要對那些家伙小心,問候!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.