我的firebase计数方法无法正常工作

时间:2017-06-29 14:03:25

标签: android firebase firebase-realtime-database

我使用firebase来计算每个用户观看的广告数量。这部分工作正常,然后当用户观看5个广告时,它解锁了应用程序的自定义。这是我计算用户广告的代码

public void count() {

    mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    uID = user.getUid();

    ref = mrootRef.child("dowload_counters").child("Adverts_Watched").getRef();
    ref.child("counter").child(uID).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {

            if (mutableData.getValue() == null) {
                mutableData.setValue(1);
            } else {

//here i grab the int to use with my rewards i think it could be something here causing the problem??
               count = mutableData.getValue(Integer.class);
                mutableData.setValue(count + 1);
            }


            return Transaction.success(mutableData);


        }

这是我的奖励方法

public void Rewards_Stuff(Context c){

    mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    uID = user.getUid();
    ref = mrootRef.child("RewardsSystem").getRef();

    if (count<5) {

//this one works and registers to my database
        Toast.makeText(c,"watch more ads to gain reward points",Toast.LENGTH_LONG).show();

        ref.child("counter").child(uID).setValue("0");

    }else
    if (count==5){

//this one wont?
        Toast.makeText(c,"New button colours unlocked from the main menu",Toast.LENGTH_LONG).show();
        ref.child("counter").setValue("1");
    }else
    if (count>5 && count<10) {

//this one wont?
        Toast.makeText(c, "Thanks for the on going support gain more reward points by watching adverts", Toast.LENGTH_LONG).show();
        ref.child("counter").child(uID).setValue("1");
    }else
    if (count==10){
        Toast.makeText(c,"background unlocked from main menu",Toast.LENGTH_LONG).show();
        ref.child("counter").setValue("2");
    }
}

为什么它没有正确地获取计数值?

0 个答案:

没有答案