Java Code Examples for com.google.firebase.database.DatabaseReference#runTransaction()

The following examples show how to use com.google.firebase.database.DatabaseReference#runTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommentInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void decrementCommentsCount(String postId, final OnTaskCompleteListener onTaskCompleteListener) {
    DatabaseReference postRef = databaseHelper
            .getDatabaseReference()
            .child(DatabaseHelper.POSTS_DB_KEY + "/" + 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);
            }
        }
    });
}
 
Example 2
Source File: PostInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void incrementWatchersCount(String postId) {
    DatabaseReference postRef = databaseHelper.getDatabaseReference().child(DatabaseHelper.POSTS_DB_KEY + "/" + 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.");
        }
    });
}
 
Example 3
Source File: ProfileInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void updateProfileLikeCountAfterRemovingPost(Post post) {
    DatabaseReference profileRef = databaseHelper
            .getDatabaseReference()
            .child(DatabaseHelper.PROFILES_DB_KEY + "/" + 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.");
        }
    });

}
 
Example 4
Source File: SignInActivity.java    From budgetto with MIT License 6 votes vote down vote up
private void runTransaction(DatabaseReference userReference) {
    showProgressView();
    userReference.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            User user = mutableData.getValue(User.class);
            if (user == null) {
                mutableData.setValue(new User());
                return Transaction.success(mutableData);
            }

            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean committed,
                               DataSnapshot dataSnapshot) {
            if (committed) {
                startActivity(new Intent(SignInActivity.this, MainActivity.class));
                finish();
            } else {
                errorTextView.setText("Firebase create user transaction failed.");
                hideProgressView();
            }
        }
    });
}
 
Example 5
Source File: CommentInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void decrementCommentsCount(String postId, final OnTaskCompleteListener onTaskCompleteListener) {
    DatabaseReference postRef = databaseHelper
            .getDatabaseReference()
            .child(DatabaseHelper.POSTS_DB_KEY + "/" + 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);
            }
        }
    });
}
 
Example 6
Source File: PostInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void incrementWatchersCount(String postId) {
    DatabaseReference postRef = databaseHelper.getDatabaseReference().child(DatabaseHelper.POSTS_DB_KEY + "/" + 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.");
        }
    });
}
 
Example 7
Source File: ProfileInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void updateProfileLikeCountAfterRemovingPost(Post post) {
    DatabaseReference profileRef = databaseHelper
            .getDatabaseReference()
            .child(DatabaseHelper.PROFILES_DB_KEY + "/" + 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.");
        }
    });

}
 
Example 8
Source File: PostListFragment.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
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 committed,
                               DataSnapshot currentData) {
            // Transaction completed
            Log.d(TAG, "postTransaction:onComplete:" + databaseError);
        }
    });
}