我正在构建一个应用,其中将使用firebase实施upvote系统,但是该应用没有用户logi0 / sign或我不想添加一个。我已经阅读了documentation on transaction()
,以了解如何在我的应用中实现升级系统,但是如果没有用户登录,我无法解决如何使用它吗?我有一个Post类,类似于Firebase文档中提供的类
public class _Post {
public String uid;
public String timeStamp;
public String author;
public String body;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public _Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public _Post(String uid, String author, String body, String timeStamp) {
this.uid = uid;
this.author = author;
this.body = body;
this.timeStamp = timeStamp;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("author", author);
result.put("body", body);
result.put("starCount", starCount);
result.put("stars", stars);
result.put("time", timeStamp);
return result;
}
public String getUid() {
return uid;
}
public String getTimeStamp() {
return timeStamp;
}
public String getAuthor() {
return author;
}
public String getBody() {
return body;
}
public int getStarCount() {
return starCount;
}
public Map<String, Boolean> getStars() {
return stars;
}
}
这是我的投票方法,也是我从firebase文档/指南中摘录的
private void onStarClicked(String postId, final String uid) {
DatabaseReference postRef = mDatabaseRef.child("north_america").child("posts").child(postId);
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
_Post p = mutableData.getValue(_Post.class);
if (p == null) {
return Transaction.success(mutableData);
}
if (p.stars.containsKey(uid)) {
// Unstar the post and remove self from stars
p.starCount = p.starCount - 1;
p.stars.remove(uid);
} else {
// Star the post and add self to stars
p.starCount = p.starCount + 1;
p.stars.put(uid, 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);
}
});
}
我遇到的另一个问题是firebase Posts节点的外观如何? 也许是这样吗? (我的应用程序谈论即将上映的电影)
-- north_america
--- posts
---- movie_id
movie_id
会是这样吗?
在此示例中,电影的ID是1123
"1123": {
"uid": "user_2",
"title": "I am legend"
"likes": 0
}
Posts节点中的Post对象将包含什么?
答案 0 :(得分:0)
如果您希望安全地标识设备/会话而不要求用户提供凭据,则可以使用Firebase's anonymous authentication。这样,您只需调用以下命令即可为当前设备上的用户创建“会话”:
FirebaseAuth.getInstance().signInAnonymously()
这可以识别客户端上的用户,(更重要的是)服务器上的用户,因此您可以确保每个用户只能投票一次。
如果您不希望限制每个用户的投票数,而只是想以交易方式增加电影的likes
,请在每次点击时执行以下操作:
private void onStarClicked(String postId, final String uid) {
DatabaseReference postRef = mDatabaseRef.child("north_america/posts").child(postId).child("likes");
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Long value = mutableData.getValue(Long.class);
if (value == null) value = new Long(0);
mutableData.setValue(value + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
}
代码仅在likes
属性上执行事务,从而增加其值。