我希望Firebase实时数据库在里面更新

时间:2018-01-17 09:06:28

标签: android firebase firebase-realtime-database

我有这样的数据库:

enter image description here

我想将cityIdealScore的值更新为" cityTop * 10 + cityWin"在firebase DB中自动生成,但我不知道该怎么做。

P.S。我需要cityIealScore的值来按分数查询数据

第2页

public void fbWinUpdate(int cityNum) {
    String cNum = String.valueOf(cityNum);
    databaseReference.child(cNum).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            CityDetails cd = mutableData.getValue(CityDetails.class);
            if(cd == null) {
                return Transaction.success(mutableData);
            }

            cd.cityWin = cd.cityWin + 1;

            mutableData.setValue(cd);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            Log.d("DBError", "postTransaction:onComplete:" + databaseError);
        }
    });
}

public void fbTopUpdate(int cityNum) {
    String cNum = String.valueOf(cityNum);
    databaseReference.child(cNum).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            CityDetails cd = mutableData.getValue(CityDetails.class);
            if(cd == null) {
                return Transaction.success(mutableData);
            }

            cd.cityTop = cd.cityTop + 1;

            mutableData.setValue(cd);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            Log.d("DBError", "postTransaction:onComplete:" + databaseError);
        }
    });
}

public void fbTop4Update(int cityNum) {
    String cNum = String.valueOf(cityNum);
    databaseReference.child(cNum).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            CityDetails cd = mutableData.getValue(CityDetails.class);
            if(cd == null) {
                return Transaction.success(mutableData);
            }

            cd.cityTop4 = cd.cityTop4 + 1;

            mutableData.setValue(cd);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            Log.d("DBError", "postTransaction:onComplete:" + databaseError);
        }
    });    }

这是我的cityTop,cityWin,cityTop4&n;的更新代码。我想我可以用这种方法计算得分,但我觉得它无效。我想知道我可以在FirebaseRules上计算得分等等

1 个答案:

答案 0 :(得分:1)

这是你可以做到的:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("City_Details_Database");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Integer cityTop = ds.child("cityTop").getValue(Integer.class);
            Integer cityWin = ds.child("cityWin").getValue(Integer.class);

            Integer cityIdealScore = cityTop*10 + cityWin;
            dataSnapshot.child("cityIdealScore").getRef().setValue(cityIdealScore);
            Log.d("TAG", valueToUpadate);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);