我正在尝试为我正在制作的应用中的帖子更新费率计数器,同时还为用户增加了一个投票计数器。执行此操作的正确方法是使用事务。但是当我运行交易时,没有任何反应。可能是什么原因?这是我的代码:
private void rate(String id, final int rateCount) {
final DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
FirebaseDatabase.getInstance().getReference("posts").child(id)
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post p = new Post("","","");
if (mutableData.getValue(Post.class) != null) {
p = mutableData.getValue(Post.class);
}
p.setPointsCount(p.getPointsCount() + rateCount);
p.rates.put(userPrefs.getId(), rateCount);
users.child(p.getUploader()).runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
User p = mutableData.getValue(User.class);
if (p == null) {
return Transaction.success(mutableData);
}
p.setPoints(p.getPoints() + rateCount);
mutableData.setValue(p);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
// Set value and report transaction success
mutableData.setValue(p);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
Log.w("Rate", "Rated Successfully! <-----|");
// Transaction completed
}
});
}