当前位置: 首页>>代码示例>>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;未经允许,请勿转载。