本文整理匯總了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));
}
});
}