【问题标题】:How to write the post's "stars" under separate database, and keep the starCount under the post?如何在单独的数据库下写入帖子的“星数”,并保留帖子下的starCount?
【发布时间】:2018-06-16 23:38:00
【问题描述】:

我正在创建一个使用 firebase 作为数据库的 android 应用程序,并且我正在尝试编写星星(其中包含盯着帖子的用户列表在单独的 firebase 数据库下,以使数据库更浅,并且帖子的快照更便宜。

目前,当用户点击星星时,首先它会添加他的 $uid 星星(如果他已经盯着帖子,则将其删除),然后相应地执行 starCount + 或 -。 “stars”和starCount都在“posts”下。

我想在单独的数据库下更新“stars/postKey/uid”,并保留帖子下的starCount。

please see the image

片段中的代码是:

    // Bind Post to ViewHolder, setting OnClickListener for the star button
            viewHolder.bindToPost(model, new View.OnClickListener() {
                @Override
                public void onClick(View starView) {
                    // Need to write to both places the post is stored
                    DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
                    DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());


                    // Run two transactions
                    onStarClicked(globalPostRef);
                    onStarClicked(userPostRef);

                }
            });

         }
    };

    mRecycler.setAdapter(mAdapter);

}//end of onCreateActivity

// [START post_stars_transaction]
private void onStarClicked(DatabaseReference postRef) {
    postRef.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            Post p = mutableData.getValue(Post.class);
            //Likes l = mutableData.getValue(Likes.class);
            if (p == null) {
                return Transaction.success(mutableData);
            }

            if (p.stars.containsKey(getUid())) {
                // Unstar the post and remove self from stars
                p.starCount = p.starCount - 1;
                p.stars.remove(getUid());
            } else {
                // Star the post and add self to stars
                p.starCount = p.starCount + 1;
                p.stars.put(getUid(), true);
            }

            // Set value and report transaction success
            mutableData.setValue(p);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b,
                               DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "postTransaction:onComplete:" + databaseError);
        }
    });
}

我已经尝试了一周但没有成功,我是编程新手,非常感谢您的帮助。

感谢@Alex 的回答,因为我想要stars/postKey/uid,我已经修改了你的建议,现在效果很好,但我还有两个问题 1) 我想从帖子中删除 stars 节点,当我这样做时,当用户取消帖子时,它会在帖子(p.stars)下查找$uid,因为星星是帖子对象的一部分,所以如果星星为空或不包含$uid, starCount 将不起作用。这是我的新代码,它将星星写入两个节点(starCount 正在工作并连接到 Post 下的星星节点)。

    private void onStarClicked(final DatabaseReference postRef) {
    postRef.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            Post p = mutableData.getValue(Post.class);
            //Likes l = mutableData.getValue(Likes.class);
            if (p == null) {
                return Transaction.success(mutableData);
            }

            if (p.stars.containsKey(getUid())) {
                // Unstar the post and remove self from stars
                p.starCount = p.starCount - 1;
                p.stars.remove(getUid());
                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                DatabaseReference starsRef = rootRef.child("stars");
                starsRef.child(postRef.getKey()).child(getUid()).setValue(null);
            } else {
                // Star the post and add self to stars
                p.starCount = p.starCount + 1;
                p.stars.put(getUid(), true);
                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                DatabaseReference starsRef = rootRef.child("stars");
                starsRef.child(postRef.getKey()).child(getUid()).setValue(true);
            }

            // Set value and report transaction success
            mutableData.setValue(p);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b,
                               DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "postTransaction:onComplete:" + databaseError);
        }
    });
}

firebasedatbase 看起来像这样

The firebasedatbase now looks like this, please see picture

我遇到的第二个问题,即下面更改星星 UI 的代码,也连接到 Post 下的星星节点。

                    // Determine if the current user has liked this post and set UI accordingly
          //  if (model.stars.containsKey(getUid())) {

            if (model.stars.containsKey(getUid())) {
                viewHolder.starView.setImageResource(com.bl3rbi.firebase.quickstart.bl3rbi.R.drawable.likes_nt_rd);
            } else {
                viewHolder.starView.setImageResource(com.bl3rbi.firebase.quickstart.bl3rbi.R.drawable.likes_nt_w);
            }
            final String postKey2 = getRef(position).getKey();

如何删除 Post 下的原始星星节点,并将星星 UI 的 starCount 和更改连接到新的星星节点。

非常感谢您对此的支持。

【问题讨论】:

    标签: java android firebase-realtime-database


    【解决方案1】:

    拥有如下所示的数据库结构:

    Firebase-root
         |
         --- stars
               |
               --- pushedId1
                      |
                      --- uid1: true
                      |
                      --- uid2: true
    

    请使用以下代码:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference starsRef = rootRef.child("stars");
    starsRef.push().child(uid).setValue(true);
    

    其中uid是你要添加到这个节点的用户的id。

    【讨论】:

    • 谢谢@Alex,我已经使用了你的代码并做了一些更改,现在它可以工作了,但是如果我删除了帖子下的星星节点,我有两个问题; starCount 不起作用,star UI 也不起作用,因为两者都连接到 Post 下的原始 stars 节点,请参见上文,我添加到我的原始问题中。
    • 它不起作用,因为有不同的参考。您需要相应地更改它们。要删除节点,您只需从 Firebase 控制台中删除它即可。删除某些内容时,之前也要复制一份。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多