Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2017 08:47
Show Gist options
  • Save anonymous/5a42cf6d79ec75f93680734ec5dcd264 to your computer and use it in GitHub Desktop.
Save anonymous/5a42cf6d79ec75f93680734ec5dcd264 to your computer and use it in GitHub Desktop.
DatabaseReference countRef = databaseReference.child("users").child(uid);
countRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
User u = mutableData.getValue(User.class);
if (u == null) {
Log.e("NULL","N");
return Transaction.success(mutableData);
}
if (u.followers.containsKey(currentUser.getUid())) {
Log.e("complete","contains");
u.follower_count = u.follower_count - 1;
u.followers.remove(currentUser.getUid());
} else {
Log.e("complete","!contains");
u.follower_count = u.follower_count + 1;
u.followers.put(currentUser.getUid(), true);
}
mutableData.setValue(u);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.e("complete","casc");
}
});
public String uid;
public String email;
public String name;
public String surname;
public String interests;
public String avatar_url;
public long following_count;
public long follower_count;
public Map<String, Boolean> followers = new HashMap<>();
public Map<String, Boolean> following = new HashMap<>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment