'My Firebase count method is not working correctly
I'm using Firebase to count the number of adverts each individual user watches. This part works fine, then when the user has watched 5 adverts it unlocks customizations to the app. This is my code for counting the users adverts
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);
}
This is my rewards method:
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 won't?
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 won't?
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");
}
}
Why isn't it grabbing the value of count correctly?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
