Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Update is Making New one With the same id except last character

I am using this code to update data on Firebase but it makes new one. I tried a lot of code and it dose the same making new one with same key except last character.

I used this as the Firebase site recommended, but it doesn't work. I created a new one and the next time its update:

    Map<String, Object> childUpdate = new HashMap<>();
childUpdate.put("/masjeds/" + masjed.getId(), masjed.toMap());
reference.updateChildren(childUpdate);

and this code done the same

     final FirebaseDatabase database = FirebaseDatabase.getInstance();
    masjeds = database.getReference("masjeds");

        reference.child(masjed.getId()).setValue(masjed, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
         //   Toast.makeText(MyMasjedsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

Masjed Class is plain java object

public class Masjed {
private String userID;
private String id;
private String name;
private String address;
private String phone;
private boolean matloopEmam;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUserID() {
    return userID;
}

public void setUserID(String userID) {
    this.userID = userID;
}

public boolean isMatloopEmam() {
    return matloopEmam;
}

public void setMatloopEmam(boolean matloopEmam) {
    this.matloopEmam = matloopEmam;
}


public Masjed(String name, String address, String phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
}

public Masjed() {

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public Map<String, Object> toMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("name", name);
    map.put("address", address);
    map.put("phone", phone);
    map.put("id", id);
    map.put("userID", userID);
    return map;
}

}

enter image description here

the id is the problem I was assuming that push.getkey and put it as ID then use push.setValue(masjed) will use the same key it turn out that it is not always the Case the Key changes when I am use it dosnt exist and so it create new one the answer that helped me is Chester Answer

like image 674
3bdoelnaggar Avatar asked Mar 02 '17 15:03

3bdoelnaggar


1 Answers

I hope this helps, used it in my pet project (it's not the cleans solution but it works):

masjeds = database.getReference("masjeds");

ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
                masjedKey = dataSnapshot1.getKey(); // This is a member variable
                masjed = dataSnapshot1.getValue(Masjed.class); // This is a member variable
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};
masjeds.orderByChild("id").equalTo(masjed.getId()).addValueEventListener(listener);

//Use your Map to update each value this is just an example
masjeds.child(masjedKey).child("name").setValue("John"); //you can add event listener if you want to see if it's completed, but it works without the events
like image 138
Chester Cobus Avatar answered Nov 01 '22 12:11

Chester Cobus