Firebase增量双倍做无限循环

时间:2018-02-19 22:14:00

标签: java android firebase firebase-realtime-database

我正在创建一个执行工作的方法,每次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) {

            }
        });

infinite loop

3 个答案:

答案 0 :(得分:0)

正如Doug在他的评论中提到的,对于这个用例,你应该使用交易。为此,我建议你绝对使用transactions。如果用户试图在同一时间增加/减少计数器,您将避免错误的结果。我已经解释了 here 如何使用事务来增加score而不用担心数据库中出现的值不正确。

答案 1 :(得分:0)

    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

你应该检查this post

答案 2 :(得分:0)

@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) {

                                    }
                                });
                    }
                });
            }
看起来像aftertextchanged创建了一个bucle,所以小心那些家伙,问候!