in database/src/main/java/com/google/firebase/quickstart/Database.java [66:91]
private static void updateStarCount(DatabaseReference postRef) {
// [START post_stars_transaction]
postRef.runTransaction(new Transaction.Handler() {
public Transaction.Result doTransaction(MutableData mutableData) {
Post post = mutableData.getValue(Post.class);
if (post != null) {
// Update the starCount to be the same as the number of members in the stars map.
if (post.stars != null) {
post.starCount = post.stars.size();
} else {
post.starCount = 0;
}
mutableData.setValue(post);
return Transaction.success(mutableData);
} else {
return Transaction.success(mutableData);
}
}
public void onComplete(DatabaseError databaseError, boolean complete, DataSnapshot dataSnapshot) {
System.out.println("updateStarCount:onComplete:" + complete);
}
});
// [END post_stars_transaction]
}