Reputation: 77
Actually, I am implementing a Firebase database to store users for my android base.
Basically, this is my User bean :
public class User {
private String idAccount;
private String firstname;
private String lastname;
private String pseudo;
private Address address;
private List<Qualification> qualification;
private boolean isWorker;
// Constructors + getters + setters
}
And, once an User is created, I add it in the database with the method :
String idUser = user.getIdAccount();
usersRef.child(idUser).setValue(user);
So, this is very simple and it works but I have a little problem. If I check the database : the user added, I can see the idAccount is duplicate.
I know this is totally normal ! But I am looking for a simple way to remove this field idAccound
in the database without explicitlu precise all the fields when I use the setValue(user)
.
Someone has a solution ?
Thank you !!
Upvotes: 1
Views: 1150
Reputation: 138824
To remove that field from your entire database, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ds.child("idAccount").getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
After that, just simply add @Exclude
annotation in front of the field in your model class like this:
@Exclude
private String idAccount;
If you are still in testing with your app, you can also remove the private String idAccount;
entirely.
Upvotes: 1
Reputation: 11267
You need to use the @Exclude
annotation.
https://firebase.google.com/docs/reference/android/com/google/firebase/database/Exclude
public class User {
@Exclude
private String idAccount;
private String firstname;
private String lastname;
private String pseudo;
private Address address;
private List<Qualification> qualification;
private boolean isWorker;
// Constructors + getters + setters
}
I would not do this, but that's how.
Upvotes: 1