java - 我想在 firebase 中管理多个用户的点赞数

标签 java android firebase firebase-realtime-database

我想要检索点赞计数器并想要将结果上传到 firebase 数据库。如果另一个用户喜欢该帖子,则喜欢计数器应该增加该值,而不是从 0 开始。

DatabaseLOLCounter.child(post_key).child(mAuth.getCurrentUser().getUid()).child("counter").setValue('like counter value here');

下面是我的观看者

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) {

                            }
                        });

                }
            });

在更新 lolCounter 中,我正在使用 inTransaction 管理器,但我不知道如何检索结果以及如何将其存储在数据库中,我已经尝试过,但它不起作用,请纠正我。

    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);
        }
    });
}

控制台给我错误“TransactionTooLargeException”

最佳答案

当在 Activity 之间传递过多的 Intent 数据时,会发生TransactionTooLargeException。它与 Firebase 无关。 Here you can read more.

关于java - 我想在 firebase 中管理多个用户的点赞数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529734/

相关文章:

android - 使用 Firebase 测试实验室为 Android 库执行检测测试?

java - 提高类的内聚和耦合

android - 向 CTS 和构建中添加新的测试包

java - Android:在蓝牙应用程序的单个 APK 上支持多个 API 级别

android - <-- android 图形中的运算符

swift - 使用 OneSignal/Firebase 从 iOS 设备发送推送通知

firebase - Firebase onWrite() 的云函数 promise 总是超时

java - 优化tomcat启动时间

Java,SocketChannel选择器,将写 channel 与阻塞队列结合起来

java - Apache POI 幻灯片注释