簡體   English   中英

Firebase增量雙做無限循環

[英]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) {

            }
        });

無限循環

正如Doug在其評論中提到的那樣,對於這種用例,您應該使用事務。 為此,我建議您一定要使用transactions 如果用戶嘗試同時增加/減少計數器,則將避免錯誤的結果。 在這里,我已經解釋如何使用事務來增加score而不必擔心在數據庫中會出現不正確的值。

    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM