I want to retrieve the like counter and want to upload result on firebase database. if another user likes the post the like counter should increment the value rather than starting from 0.
DatabaseLOLCounter.child(post_key).child(mAuth.getCurrentUser().getUid()).child("counter").setValue('like counter value here');
below is my viewholder
viewHolder.mLOL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processLOL=true;
DatabaseFacepalm.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
DatabaseAngry.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
DatabaseNeutral.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
DatabaseLOL.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processLOL)
{
if(dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid()))
{
//Toast.makeText(MainActivity.this,mAuth.getCurrentUser().getUid(),Toast.LENGTH_LONG).show();
//if already reacted
//DatabaseLOLCounter.setValue(Count = Count - 1 );
updateLOLCounter(false,post_key);
DatabaseLOL.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
processLOL=false;
}
else
{
//if no react
updateLOLCounter(true,post_key);
//DatabaseLOLCounter.setValue(Count = Count + 1 );
DatabaseLOL.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue("lol");
processLOL=false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
In update lolCounter I'm using the inTransaction manager butI dont know how to retrieve sesults and how to store it in data base, I've tried but it's not working, please correct me.
private void updateLOLCounter(final boolean increment, final String post_key) {
DatabaseLOLCounter.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
if (mutableData.getValue() != null) {
int value = mutableData.getValue(Integer.class);
if(increment)
{
value++;
} else {
value--;
}
mutableData.setValue(value);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Is the below method is right?
DatabaseLOLCounter.child(post_key).child(mAuth.getCurrentUser().getUid()).child("counter").setValue(dataSnapshot);
// Toast.makeText(MainActivity.this,dataSnapshot,Toast.LENGTH_LONG).show();
// Transaction completed
//Log.d(TAG, "likeTransaction:onComplete:" + databaseError);
}
});
}
The console is giving me error "TransactionTooLargeException"