Kai
Kai

Reputation: 105

Deleting data and immediately creating a new one

I am deleting user's data in realtime database. However, whenever I am trying to delete it, there's this one child that remains. I don't know why it won't delete it, even though I have tried using .removeValue().

Here's the image of my database

enter image description here

as you can see the image above ( orange-ish line ) that is the data of the user, and every time I want to delete that user, the "termsAgreement" child remains, and here's my code on removing the whole child.

holder.btndel_stud.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Delete Student Record");
                alert.setMessage("Are you sure you want to delete");

                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
                        final String uniqueKey = addingStudentsArrayList.get(position).getUniqueid();
                        Toast.makeText(context, uniqueKey, Toast.LENGTH_SHORT).show();
                        databaseReference.child(uniqueKey).removeValue();
                        Toast.makeText(context, "Student has been deleted, re-authenticate to delete the Registered Email", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                alert.show();
            }
        });

so basically the user have to agree on terms and condition first and this is my code whenever the user will agree on the terms and conditions.

checkBoxAgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AgreementBtn.setEnabled(isChecked);

                AgreementBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        FirebaseAuth userAuth = FirebaseAuth.getInstance();
                        final String firebaseUser = userAuth.getCurrentUser().getUid();

                        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser);
                        databaseReference.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                databaseReference.child("termsAgreement").setValue("yes");
                                Toast.makeText(TermsAndConditionActivity.this, "You have agreed to Terms and Condition", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(getApplicationContext(), WelcomeStudentActivity.class));
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                    }
                });
            }
        });

I really don't know why the termsAgreement remains even tho I use .removeValue() to the unique id wherein the information was saved.

Upvotes: 0

Views: 43

Answers (2)

samthecodingman
samthecodingman

Reputation: 26196

When a user logs in, you get them to agree to the terms agreement. When they click the "AgreementBtn", it writes "yes" to /Users/{USER_ID}/termsAgreement. However, when this "AgreementBtn" is clicked, it doesn't check if this user's data has been deleted first - it just writes the value anyway.

final DatabaseReference currentUserDataRef = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser);
currentUserDataRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (!snapshot.exists()) {
            // this user's data has been deleted/doesn't exist yet
            // TODO: do something, like re-register user
        } else {
            currentUserDataRef.child("termsAgreement").setValue("yes")
                .addOnCompleteListener(TermsAndConditionActivity.this, new OnCompleteListener<Void> {
                    @Override
                    public void onComplete(Task<Void> task) {
                        /* TODO: listen for success/failure events here */
                        if (task.isSuccessful()) {
                            Toast.makeText(TermsAndConditionActivity.this, "You have agreed to Terms and Condition", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(), WelcomeStudentActivity.class));
                        } else {
                            // TODO: Handle error in task.getException()
                            Toast.makeText(TermsAndConditionActivity.this, "Error when agreeing to terms: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        // TODO: Handle this error, don't ignore it.
        Toast.makeText(TermsAndConditionActivity.this, "Error when fetching user data: " + error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

So it appears there's nothing inherently wrong with your code, but one with the flow of events. If a user's account is deleted, you prompt them to login with that email, which when they do log in, it writes termsAgreement = "yes" to where their user data was deleted.

Note: Don't use databaseReference as a variable name. Use something meaningful such as usersRef (for /Users) or currentUserDataRef (for /Users/{USER_ID}) as appropriate.

Upvotes: 1

Emon Hossain Munna
Emon Hossain Munna

Reputation: 755

So the problem is with your terms & conditions database ref:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser);

Which is different from when you are deleting the data:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");

They are not the same database ref.

That's why when you are trying to use removeValue() it's not deleting at the right location.

Upvotes: 0

Related Questions