1

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

enter image description here

Edit: Database structure should look like this

enter code here
DB
 └attr_reviews
             └city1
                  └useruuid1
                           └comment: ""
                           └rating: x
                  └useruuid2
                           └....
            └city2
                 └....
4
  • Changing the db reference didn't work? Commented Jan 24, 2022 at 17:18
  • It writes data, but not how it's supposed to be, it delete all other childs (other uuids as well with comment and rating) Commented Jan 24, 2022 at 17:34
  • As a temporary fix you could retrieve the previous values before you push and push everything in on the next push. Commented Jan 24, 2022 at 17:41
  • Where have you reached on this issue? Commented Jan 26, 2022 at 18:06

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank's on help this is what i needed
0

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

5 Comments

This example above isn't working properly, imgur.com/ovEUXeM data deleted after inserting a new one
It's getting deleted because of the first setValue that you are doing. Is uuid equal to Sarajevo?
Nah, Sarajevo isn't UUID, Sarajevo is city name, like many other, this cities should have child UserUUID, which represents user review and contains Review, and Rating. Sarajevo should contain childs with UUID
remove the first setvalue db.setValue(uuid).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { it is not needed
It's needed beacuse user UUID dont exist as child, and it's needed later to check did user gave review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.