I have to update my code and implement another child update to a node by increasing its value by 1. My current code looks like this (which works fine):
String key = database.child("rides").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/rides/" + key, ride.toMap());
childUpdates.put("/ridesUser/" + ride.getUserID() + "/ownRides/" + key, ride.getTime());
database.updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
}
});
An easy way to add the increment code would be:
childUpdates.put("/stations/" + ride.getFromID() + "/rideCount", fromStation.getRideCount() + 1);
But fromStation.getRideCount()
could already be outdated, if another user added a ride with this station.
I've read several posts about incrementing values by using a transaction. How can I combine child updates and transactions? Should I run a transaction on "/stations/" + ride.getFromID()
and do my children updates within the doTransaction()
method? Is this atomically?