Home > Enterprise >  Cannot cancel nodes from Firebase Database
Cannot cancel nodes from Firebase Database

Time:02-01

I am developing an app and I have a problem. I started recording GPS position, but data started to be too much. So I implemented a Button to cancel anything is under "Location" (or under "latitude" and "longitude", it is the same for me). The problem is that nothing seems to be canceled into the database. I am facing this problem for 2 days and I am stuck. This is my DB:

This is my database

This is the code I used:

public class GalleryFragment extends Fragment {
    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;
    DatabaseReference mbase;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        EditText ET = binding.EditProva;
        Button btn = binding.buttonProva;
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        mbase = FirebaseDatabase.getInstance().getReference();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference drTest = FirebaseDatabase.getInstance().getReference();
                drTest.child("Location").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for(DataSnapshot dataSnapshot:snapshot.getChildren()){
                            dataSnapshot.getRef().setValue(null);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        Log.w("Errore DB: ", error.getMessage());
                    }
                });
            }
        });

        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                //textView.setText(s);
            }
        });
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
   }

Before this solution I used that code in the :

                DatabaseReference latRef = database.getReference("Location/latitude");
                latRef.removeValue();
                DatabaseReference longRef = database.getReference("Location/longitude");
                longRef.removeValue();

But neither does this work. I checked out other answers, but nothing helps me.

CodePudding user response:

The problem is that nothing seems to be canceled into the database.

That's happening most likely because you are trying to delete a huge amount of data. If you want to remove everything that exists under the Location node, then you should consider deleting a reasonable number of items one at a time. That can be done on the client, but you can also write a function in Cloud Functions for Firebase, to achieve the exact same result.

  •  Tags:  
  • Related