本文整理汇总了Java中com.google.firebase.database.DatabaseReference.runTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java DatabaseReference.runTransaction方法的具体用法?Java DatabaseReference.runTransaction怎么用?Java DatabaseReference.runTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.firebase.database.DatabaseReference
的用法示例。
在下文中一共展示了DatabaseReference.runTransaction方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runPostViewsCountTransition
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
public static void runPostViewsCountTransition(DatabaseReference postRef) {
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post post = mutableData.getValue(Post.class);
if (post == null) {
return Transaction.success(mutableData);
}
int viewsCount = post.getViews() + 1;
post.setViews(viewsCount);
mutableData.setValue(post);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
showLog("runCommentsCountTransition " + (databaseError != null));
/* if (databaseError == null) {
errorListener.onError();
}*/
}
});
}
示例2: updateProfileLikeCountAfterRemovingPost
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
public void updateProfileLikeCountAfterRemovingPost(Post post) {
DatabaseReference profileRef = database.getReference("profiles/" + post.getAuthorId() + "/likesCount");
final long likesByPostCount = post.getLikesCount();
profileRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue != null && currentValue >= likesByPostCount) {
mutableData.setValue(currentValue - likesByPostCount);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating likes count transaction is completed.");
}
});
}
示例3: decrementCommentsCount
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
public void decrementCommentsCount(String postId, final OnTaskCompleteListener onTaskCompleteListener) {
DatabaseReference postRef = database.getReference("posts/" + postId + "/commentsCount");
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue != null && currentValue >= 1) {
mutableData.setValue(currentValue - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating comments count transaction is completed.");
if (onTaskCompleteListener != null) {
onTaskCompleteListener.onTaskComplete(true);
}
}
});
}
示例4: incrementWatchersCount
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
public void incrementWatchersCount(String postId) {
DatabaseReference postRef = database.getReference("posts/" + postId + "/watchersCount");
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(1);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
LogUtil.logInfo(TAG, "Updating Watchers count transaction is completed.");
}
});
}
示例5: onStarClicked
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
private void onStarClicked(DatabaseReference postRef) {
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(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);
}
});
}
示例6: runCommentsCountTransition
import com.google.firebase.database.DatabaseReference; //导入方法依赖的package包/类
/**
* Decrement or increment commentsCount
*
* @param postRef the Post reference to run transaction
* @param increment if TRUE it wil increment, if FALSE it will decrement.
*/
public static void runCommentsCountTransition(DatabaseReference postRef, final boolean increment) {
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post post = mutableData.getValue(Post.class);
if (post == null) {
return Transaction.success(mutableData);
}
int commentsCount = post.getCommentsCount();
int data = increment ? commentsCount + 1 : commentsCount - 1;
post.setCommentsCount(data);
//update localy
RepositoryManager.getInstance()
.getPostsRepository()
.updateLocaly(post);
mutableData.setValue(post);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
showLog("runCommentsCountTransition: " + (databaseError != null));
}
});
}