1

I am working on a Bus booking app.So whenever a user books a ride I will store his credentials(name,email) for that particular ride.But I also need to restrict the number of bookings for that ride(like only 20 per ride).To do this I am using firebase transactions.Initially i have the value at location mref1 as 0(zero),then i updated it using transactions,but when i run my code,for the very first time it doesn't get updated and afterwards it starts updating. Can anyone tell me how? Below is my code for database(mref1 is the location where I want to store the number of bookings)My Database structure`

private DatabaseReference mDatabase1;
private DatabaseReference mDatabase2;
private DatabaseReference mref1;
private DatabaseReference mref2;
private FirebaseAuth mAuth;
private static final String TAG = "BookingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_booking);

    mAuth = FirebaseAuth.getInstance();

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDatabase1 = FirebaseDatabase.getInstance().getReference().child("Time1");
    mDatabase2 = FirebaseDatabase.getInstance().getReference().child("Time2");
    mref1 = FirebaseDatabase.getInstance().getReference().child("Count@Time1");
    mref2 = FirebaseDatabase.getInstance().getReference().child("Count@Time2");

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Book(mDatabase1,mref1);
        }
    });

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Book(mDatabase2,mref2);
        }
    });

}


public void Book(DatabaseReference mDatabase,DatabaseReference mref) {

    final FirebaseUser user = mAuth.getCurrentUser();
    HashMap<String,String>datamap = new HashMap<>();

    if(user!=null) {
        datamap.put("Name", user.getDisplayName());
        datamap.put("Email", user.getEmail());
    }

    mDatabase.push().setValue(datamap);
    Update(mref);
    Toast.makeText(BookingActivity.this, "Booked Successfully", Toast.LENGTH_SHORT).show();

}

public void Update(DatabaseReference mDatabase) {

    mDatabase.runTransaction(new Transaction.Handler() {
        @NonNull
        @Override
        public Transaction.Result doTransaction(@NonNull MutableData mutableData) {

            Integer CurrentValue = mutableData.getValue(Integer.class);
            mutableData.setValue(CurrentValue+1);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {

            Log.d(TAG, "Updating count transaction is completed.");

        }
    });
}

}

1 Answers1

1

According to the anwer from this post and seeing your code, to solve the issue, I recommend you first to check nullity using the following line of code:

if(CurrentValue != null) {}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193