【发布时间】:2018-06-16 23:38:00
【问题描述】:
我正在创建一个使用 firebase 作为数据库的 android 应用程序,并且我正在尝试编写星星(其中包含盯着帖子的用户列表在单独的 firebase 数据库下,以使数据库更浅,并且帖子的快照更便宜。
目前,当用户点击星星时,首先它会添加他的 $uid 星星(如果他已经盯着帖子,则将其删除),然后相应地执行 starCount + 或 -。 “stars”和starCount都在“posts”下。
我想在单独的数据库下更新“stars/postKey/uid”,并保留帖子下的starCount。
片段中的代码是:
// 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