Syyam Noor
Syyam Noor

Reputation: 67

I want to manage like count for multiple users in firebase

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"

Upvotes: 0

Views: 156

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

TransactionTooLargeException is thrown when huge amount of data is getting exchanged between a service and an application. This also can occur, when you pass lot of data through intent extras.

To handle this exception you need analyze your code. If possible, try to split the big operation in to small chunks. Also, try not to exchange huge data (>1MB) between services and application.

See more details of TransactionTooLargeException.

Upvotes: 0

Uriel Frankel
Uriel Frankel

Reputation: 14612

TransactionTooLargeException happens when passing a too much data on the intent between activities. It is not related to Firebase. Here you can read more.

Upvotes: 1

Related Questions