简体   繁体   中英

Firebase Realtime Database overwrites old data while adding new

Hi there i'm facing issue that FB realtime DB deletes old data while inserting a new one here is my code

 String text = reviewText.getText().toString();
                if (text.length() > 15){
                    int rating = (int) ratingBar.getRating();
                    String uuid = firebaseAuth.getCurrentUser().getUid();
                    Map<String, String> data = new HashMap();
                    data.put("rating", String.valueOf(Integer.parseInt(String.valueOf(rating))));
                    data.put("comment", text);
                    DatabaseReference  db = firebaseDatabase.getReferenceFromUrl("dburl").child("attr_reviews").child(placename);
                    db.setValue(uuid).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                db.child(uuid).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()){

                                        }else {
                                            Toast.makeText(getActivity(), R.string.eReviewErrUnkw, Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                            }else {
                                Toast.makeText(getActivity(), R.string.eReviewErrUnkw, Toast.LENGTH_LONG).show();
                            }
                        }
                    });

                }else {
                    Toast.makeText(getActivity(), R.string.eReviewErrTxt, Toast.LENGTH_LONG).show();

                }

I have tried to put push() into diffrent positions but still same. DB structure looks like this It should create DB structure like this on image

在此处输入图像描述

Edit: Database structure should look like this

enter code here
DB
 └attr_reviews
             └city1
                  └useruuid1
                           └comment: ""
                           └rating: x
                  └useruuid2
                           └....
            └city2
                 └....

The data in the database is getting overwritten, what you need to do is use push() which would create a random id for you in the database:

db.child(uuid).push().setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() {

This way you will have the following database:

uuid
  random-id1
       comment: "comment"
       rating: 1
  random-id2
       comment: "comment"
       rating: 1

In this way you stop at "Sarajevo"

DatabaseReference  db = firebaseDatabase.getReferenceFromUrl("dburl").child("attr_reviews").child(placename);
//Sarajevo
// ==> PUSHING VALUES
                    db.setValue(uuid).addOnCompleteListener(new OnCompleteListener<Void>() { ...

I think you should go down nesting. Something like

DatabaseReference  db = firebaseDatabase.getReferenceFromUrl("dburl").child("attr_reviews").child(placename).child(userid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM