I created a simple database, when the user enters new data a database is created to calculate the number of posts entered by the user and it works well in the add-on, the problem is that during the deletion process the database is not updated by decreasing the value, i.e. if it was 1 and when deleting, it is supposed to become 0, for those who do not, the value remains 1
This code is in case of addition
private void Poste(){
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference countRef = FirebaseDatabase.getInstance()
.getReference("dbStatis")
.child("Clinics")
.child(userId);
countRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
// Handle the case where the value is initially null
currentValue = 0;
}
// Decrement the value
int newValue = Math.max(0, currentValue + 1);
mutableData.setValue(newValue);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError error, boolean committed,
DataSnapshot currentData) {
if (error != null) {
Log.w(TAG, "Transaction failed:", error.toException());
} else {
Log.d(TAG, "Transaction committed.");
}
}
});
}
i call code in onCreate Poste(); If the addition is successful
on remove data
private void DeleteData(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete the recording permanently?");
builder.setPositiveButton("نعم", (dialog, which) -> {
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("dbClinics");
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReferenceFromUrl(imageUrl);
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
reference.child(key).removeValue();
PosteDes();
Toast.makeText(DetailClinic.this, "Permanently deleted", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), HomeClinics.class));
AnimationOut();
finish();
}
});
});
builder.setNegativeButton("no", (dialog, which) -> {
dialog.dismiss();
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void PosteDes(){
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference countRef = FirebaseDatabase.getInstance()
.getReference("dbStatis")
.child("Clinics")
.child(userId);
countRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
// Handle the case where the value is initially null
currentValue = 0;
}
// Decrement the value
int newValue = Math.max(0, currentValue - 1);
mutableData.setValue(newValue);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError error, boolean committed,
DataSnapshot currentData) {
if (error != null) {
Log.w(TAG, "Transaction failed:", error.toException());
} else {
Log.d(TAG, "Transaction committed.");
}
}
});
}
i call code in onCreate PosteDes(); If the Remove is successful
Ougmosta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.