Please clarify my doubt, I am working on mobile app development which uses the Firebase realtime database. url is / req/ 12345/ confirmed = ""
Scenario is: when 2 mobiles at a time app (internally using Firebase) read the Firebase node data (12345/confirmed) and check whether the data is null or empty , if it is null or empty, it will update with its id(app user have unique id)
Issue is: at the same time, both are reading data as null and both are updating the value, how to avoid this kind of scenario? Do we have any mechanism to lock that node ?
this is the code using.
String node = "/" + "req" + "/" + requestId;
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String driverIdd = preference.getLongDetail(Constants.DRIVER_ID);
Log.e("Test","Log::Going to Confirm Driver:::"+driverIdd);
String confirmed_walker = dataSnapshot.child("confirmed_walker").getValue(String.class);
Log.e("Test","Log::Confiremed::"+confirmed_walker);
if(confirmed_walker.isEmpty()){
Map<String, Object> updates = new HashMap<>();
updates.put(node + "/confirmed_walker",driverIdd);
databaseReference.updateChildren(updates).addOnCompleteListener(task -> {
Log.e("Test","Log::Confirmed Driver:::"+driverIdd);
}).addOnFailureListener(e -> {
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
databaseReference.child(
node).
addListenerForSingleValueEvent(postListener);