Reputation: 760
Hi Please find the structure of firebase DB?
Now let's say, we want to update fName and lName from -LcKJ14wv-Y46QER1V- 0
How to update them so only these values are updated and no new values are added in the database?
Code we have written
queryToGetData = FirebaseDatabase.getInstance().getReference().child(AppData.STUDENTS)
.orderByChild("studentUnique").equalTo(user.getUid()).limitToFirst(1);
queryToGetData.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
db.push().setValue(ss);
Common.alertDialog("Added","Profile Details saved", StudentProfile.this);
} else {
db.setValue(ss);
Common.alertDialog("Oops","Profile Updated", StudentProfile.this);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Common.alertDialog("Oops","Something went wrong!", StudentProfile.this);
}
});
What this piece of code is doing?
when running update for the first time(the whole collection is empty)
*when running update for the second time(same user)
when running update for the third time(same user)
Upvotes: 1
Views: 956
Reputation: 138824
I agree with what @elbert rivas said regarding the use of the uid
to identify your users rather than a pushed id. In that case, to update a user object, you don't need a query. However, if can't change your database structure, you can still update a user object like in the following lines of code:
queryToGetData = FirebaseDatabase.getInstance().getReference().child(AppData.STUDENTS)
.orderByChild("studentUnique").equalTo(user.getUid());
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Map<String, Object> map = new HashMap<>();
map.put("fName", "newFirstName");
map.put("lName", "newLastName");
ds.getRef().updateChildren(map);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
queryToGetData.addListenerForSingleValueEvent(valueEventListener);
See, I have removed the .limitToFirst(1)
because the uid
of a user are unique. So even if we remove that method call, you'll still get a single result.
Upvotes: 0
Reputation: 1464
First I would suggest to restructure your db like this for much simpler query.
students
QslkALDJFLDFLFk(this is the student unique key or the uid)
emailAddress:
fName:
Then use updateChildren()
to update specific fields.
Student student = new Student("firstName", "lastName");
Map<String, Object> values = student.toMap();
queryToGetData = FirebaseDatabase.getInstance().getReference()
.child(AppData.STUDENTS).child(user.getUid())
.updateChildren(values);
You can read the documentation here
Upvotes: 1
Reputation: 1976
i am doing this way ;-
DatabaseReference receiverUserRef = FirebaseDatabase.getInstance().getReference(chatListTableName)
.child(userId)
.child(firebaseUser.getUid());
receiverUserRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
receiverUserRef.child("time").setValue(String.valueOf(System.currentTimeMillis()));
} else {
receiverUserRef.child("id").setValue(firebaseUser.getUid());
receiverUserRef.child("isGroup").setValue("false");
receiverUserRef.child("time").setValue(String.valueOf(System.currentTimeMillis()));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 0