I'm having a problem when I try to star a movie which id doesn't exist in my likes
firebase node. Here's my code:
private void onStarClicked(long releaseId, final String uid) {
final DatabaseReference postRef = ((GamePageActivity)getActivity()).mDatabaseRef.child("likes").child(mRegion).child(String.valueOf(releaseId));
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
_Post p = mutableData.getValue(_Post.class);
if (p == null) {
Log.d(TAG, "Transaction success");
return Transaction.success(mutableData);
}
if (p.stars.containsKey(uid)) {
// Unstar the post and remove self from stars
p.starCount = p.starCount - 1;
p.stars.remove(uid);
} else {
// Star the post and add self to stars
p.starCount = p.starCount + 1;
p.stars.put(uid, 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);
}
});
}
The problem is if my id "releaseId" doesn't exist in the database, the star won't get added in, I thought my code was supposed to first add the "releaseId" if it doesn't exist?