當前位置: 首頁>>代碼示例>>Java>>正文


Java DatabaseReference.runTransaction方法代碼示例

本文整理匯總了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();
                    }*/
        }
    });
}
 
開發者ID:pedromassango,項目名稱:Programmers,代碼行數:27,代碼來源:Worker.java

示例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.");
        }
    });

}
 
開發者ID:rozdoum,項目名稱:social-app-android,代碼行數:23,代碼來源:DatabaseHelper.java

示例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);
            }
        }
    });
}
 
開發者ID:rozdoum,項目名稱:social-app-android,代碼行數:23,代碼來源:DatabaseHelper.java

示例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.");
        }
    });
}
 
開發者ID:rozdoum,項目名稱:social-app-android,代碼行數:22,代碼來源:DatabaseHelper.java

示例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);
        }
    });
}
 
開發者ID:braulio94,項目名稱:Quadro,代碼行數:33,代碼來源:PostListFragment.java

示例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));
        }
    });
}
 
開發者ID:pedromassango,項目名稱:Programmers,代碼行數:36,代碼來源:Transations.java


注:本文中的com.google.firebase.database.DatabaseReference.runTransaction方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。