skip to Main Content

I want to delete this record ? enter image description here

Because I don’t know what exactly it is, I want to know what it is… I expect that it is a special key for every data I upload it creates it automatically How do I delete and what is this key?

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("poll_post").child(firebaseUser.getUid());
        HashMap<String, Object> hashMap = new HashMap<>();
        
        String postid = reference.push().getKey();

        hashMap.put("postid", postid);
        hashMap.put("time_post", ServerValue.TIMESTAMP);
        hashMap.put("stopcomment", checked);

        hashMap.put("tv_question", addcomment.getText().toString());
        hashMap.put("tvoption1", Answer1.getText().toString());
        hashMap.put("tvoption2", Answer2.getText().toString());
        hashMap.put("vote1","0");
        hashMap.put("vote2", "0");

        hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());

        reference.push().setValue(hashMap, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                //Problem with saving the data
                if (databaseError != null) {


                    Toast.makeText(Write_poll.this, "Error", Toast.LENGTH_SHORT).show();
                    myLoadingButton.showErrorButton();

                } else {


                    myLoadingButton.showDoneButton();
              finish();
                }

            }
        });

2

Answers


  1. refer to firebase documentation and read about the function called push , they clearly said that

    push : Add to a list of data in the database. Every time you push a new node onto a list, your database generates a unique key, like messages/users//

    so that’s means that every time you are pushing to the database , a new record with a unique ID will be generated every you push and you will not be able to override a record using the function push as every time it’s called , it will generate a unique ID that you don’ want in your case.

    to avoid that , instead of

    reference.push().setValue(hashMap, new DatabaseReference.CompletionListener(){ . . . }
    

    write :

    reference.setValue(hashMap, new DatabaseReference.CompletionListener(){ . . . }
    
    Login or Signup to reply.
  2. If you want to delete a record from the Realtime Database, you have to create a reference that points to that node. In your particular case it would be:

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    db.child("poll_post").child("-NBcO...y8cX").removeValue();
    

    If you don’t know the key, then you have to create a query to find that post based on something that uniquely identifies it, for example, the time_post. So here is the code:

    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    Query queryByTime = db.child("poll_post").orderByChild("time_post").equalTo(1662830174410);
    queryByTime.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                for (DataSnapshot ds : task.getResult().getChildren()) {
                    ds.getRef().removeValue();
                }
            } else {
                Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
            }
        }
    });
    

    You can also attach a listener to the removeValue() operation to see if something goes wrong.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search