I am trying to update the value of daysOnWhichPresent
field by 1 of Firebase database. Here I have read the value two times before and after updating the value of daysOnWhichPresent
but it is not correctly reading from the database. In first read the value should be 6 but after fetching the data its value is 0.
Also this code is not letting me update the value of daysOnWhichPresent
,
the permission is set to read/write to all.
Please help me.
Here is the Firebase Database Tree
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class AttendanceActivity extends AppCompatActivity {
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
String uniqueVolunteerId;
long daysPresent;
TextView textAttendance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
Bundle bundle = getIntent().getExtras();
uniqueVolunteerId = bundle.getString("uniqueVolunteerId");
Log.i("TAG", uniqueVolunteerId);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("volunteers").child(uniqueVolunteerId);
textAttendance = findViewById(R.id.text_attendance);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
daysPresent = dataSnapshot.child("daysOnWhichPresent").getValue(Long.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("TAG", "Error in data fetching from server 1");
}
});
Log.i("TAG", Long.toString(daysPresent));
databaseReference.child("daysOnWhichPresent").setValue(daysPresent+1);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
daysPresent = dataSnapshot.child("daysOnWhichPresent").getValue(Long.class);
Log.i("TAG", Long.toString(daysPresent));
textAttendance.setText(Long.toString(daysPresent));
Toast.makeText(getApplicationContext(), Long.toString(daysPresent), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("TAG", "Error in data fetching from server 1");
}
});
}
}
The log shows that the value of daysPresent is 0, 1 and 6 But it should be 6, 7 and 7. Please help me.
As suggested by Alex Mamo, I have changed the code as follows...
package com.aayush.reboot.nssnitdurgapur;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.MutableData;
import com.google.firebase.database.Transaction;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class AttendanceActivity extends AppCompatActivity {
String uniqueVolunteerId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
Bundle bundle = getIntent().getExtras();
uniqueVolunteerId = bundle.getString("uniqueVolunteerId");
incrementAttendanceByOne(uniqueVolunteerId);
DatabaseReference volunteerRef = FirebaseDatabase.getInstance().getReference("volunteers").child(uniqueVolunteerId).child("daysOnWhichPresent");
volunteerRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Long daysPresent = dataSnapshot.getValue(Long.class);
Log.i("TAG", "Value of daysOnWhichPresent in onCreate method "+ Long.toString(daysPresent));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void incrementAttendanceByOne(String uniqueVolunteerId) {
Log.i("TAG", "Unique ID: "+uniqueVolunteerId);
DatabaseReference volunteerRef = FirebaseDatabase.getInstance().getReference("volunteers").child(uniqueVolunteerId);
DatabaseReference daysPresentRef = volunteerRef.child("daysOnWhichPresent");
daysPresentRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Long daysPresent = mutableData.getValue(Long.class);
if (daysPresent == null) {
Log.i("TAG", "Days Present is null---"+Long.toString(daysPresent));
return Transaction.success(mutableData);
}
Log.i("TAG", "Old value in function incrementAttendanceByOne "+Long.toString(daysPresent));
mutableData.setValue(daysPresent+1);
daysPresent = mutableData.getValue(Long.class);
Log.i("TAG", "Updated value in function incrementAttendanceByOne "+Long.toString(daysPresent));
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
}
I have printed value of daysOnWhichPresent
before and after the execution of transaction which shows the correct result.
But when i print the value in 'onCreate()' method after execution of incrementAttendanceByOne()
a single log statement is giving me two values first updated one and later the old value, which tells that the data has been changed in database for a moment and again it reverted back to its previous value.
What should be the problem and how to solve this?
Here is the Logcat
05-22 10:51:44.000 22737-22737/com.aayush.reboot.nssnitdurgapur I/TAG: Unique ID: G8ho4gth5ZPU7z8Sk9uK40QbGtq1
05-22 10:51:44.010 22737-22811/com.aayush.reboot.nssnitdurgapur I/TAG: Old value in function incrementAttendanceByOne 25 Updated value in function incrementAttendanceByOne 26
05-22 10:51:44.066 22737-22737/com.aayush.reboot.nssnitdurgapur I/TAG: Value of daysOnWhichPresent in onCreate method 26
05-22 10:51:44.645 22737-22737/com.aayush.reboot.nssnitdurgapur I/TAG: Value of daysOnWhichPresent in onCreate method 25