Reputation: 87
I have followers in my app. If someone follows someone it creates a new entry for the user that is being followed with a child containing the User ID of the follower.
If an User deletes their account I want that all entries of their User ID gets deleted. Is this possible?
Upvotes: 1
Views: 130
Reputation: 125
If the user deletes the account then its UID should be deleted from the whole firebase data structure so you can do the following: If the user which is going to delete its account is logged in then do following: You can give a Button named Delete Account and implement following piece of code on onclicklistener of that Button.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference followRef = FirebaseDatabase.getInstance().getReference().child("Follow").child(user.getUid()).child("following");
followRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
//Getting User id from dataSnapshot
String userid = data.getValue(String.class);
DatabaseReference Ref = FirebaseDatabase.getInstance().getReference().child("Follow").child(userid).child("followers").child(user.getUid());
Ref.removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i(TAG, "onCancelled: Error: " + databaseError.getMessage());
}
});
DatabaseReference followRef1 = FirebaseDatabase.getInstance().getReference().child("Follow").child(user.getUid());
followRef1.removeValue();
DatabaseReference followRef2 = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid());
followRef2.removeValue();
If any Query then please let me know Thank You.
Upvotes: 1
Reputation: 4035
I assume you want to remove the UID of the follower
from any possible followed user
:
If that is what you want, then you have to read the Follow
node, and remove any possible existance of the follower UID
:
//the user ID
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
//the main reference node "Follow"
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Follow");
//read the node and loop through all UIDs
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop through all UIDs under "Follow" and remove the user ID that deleted his/here account from "followers"
for(DataSnapshot ds : dataSnapshot.getChildren()){
if(ds.child("followers").child(userID).exists()){
ds.child("followers").child(userID).getRef().removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mDatabase.addValueEventListener(listener);
Upvotes: 1
Reputation: 80924
To delete the key, try the following:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference followRef = FirebaseDatabase.getInstance().getReference().child("Follow");
followRef.child(user.getUid()).removeValue();
Get the currently logged in user, and then call removeValue()
to delete the node.
Upvotes: 0