I am new to mobile android development. I am trying to build an library management application. Here I have two admins ,who can modify the database (realtime database, firebase).I tried the following code to handle the case when both admins try to alter the database simultaneously:
int books= Integer.parseInt(addBooks.getText().toString());
String isbn=isbnNo.getText().toString();
DatabaseReference ref=reference.child("books").child(isbn).child("bookCount");
ref.runTransaction(new Transaction.Handler() {
@NonNull
@Override
public Transaction.Result doTransaction(@NonNull MutableData mutableData) {
Integer currentBookCount=mutableData.getValue(Integer.class);
mutableData.setValue(currentBookCount+books);
return Transaction.success(mutableData);
}
@Override
public void onComplete(@Nullable DatabaseError error, boolean committed, @Nullable DataSnapshot currentData) {
Toast.makeText(getApplicationContext(),"Added successfully",Toast.LENGTH_LONG).show();
}
});
My database looks like this:
It shows "Added successsfully" messages in both the devices when I run simultaneously, but is not upadating the final answer in the database.I am also not sure if this approach is appropriate, any help is appreciated.