0

I have the following chunk of code:

FirebaseDatabase database = FirebaseDatabase.getInstance();
imgScanStatus = database.getReference().child("imgScanStatus");
imgScanStatus.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        Map<String, Object> scanStatus = (Map<String, Object>) dataSnapshot.getValue();
                        if(scanStatus.get("isFinishedScanning").equals(true)){
                            downloadFile(ScanResult.this, "scan", ".txt", DIRECTORY_DOWNLOADS, url);
                        }
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

Whenever there is a data change in the database, the code checks if isFinishedScanning is true, if satisfied it downloads a file. This works fine. Now when I run the app again, the next time around it downloads 2 of the same file. On the third run, it downloads it 3 of the same file and so on. I am guessing it is because the method registers the cumulative number of data changes, meaning value of isFinishedScanning was set to true a total of 3 times by the third time I run the function in the app. How do I set it up so that it checks the value and only downloads the file once no matter how many times I run the app?

Here's my database structure: enter image description here

turing042
  • 99
  • 10
  • Have you tried to remove the listener when you press back – aryanknp Sep 16 '20 at 06:15
  • You can check **[this](https://stackoverflow.com/questions/48699032/how-to-set-addsnapshotlistener-and-remove-in-populateviewholder-in-recyclerview/)** out. – Alex Mamo Sep 16 '20 at 07:16

2 Answers2

0

You should arrange for every call to addValueEventListener to be matched by a call to removeEventListener to remove the listener that was added. If you add a listener and don't remove it when you're done, it will continue to listen and receive callbacks until the app process dies. This removal does not happen automatically when the user leaves the screen - you will have to write you code to handle that. You might want to use the Android lifecycle to manage the listener correctly.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

sir you have to use the addChildEventListener. In this whenever new file is added in your node it will only trigger the onChildAdded method and give you the data which is added.

DatabaseReference myFirebaseRef = database.getReference("Your firbase path");
myFirebaseRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    /*
      here you can get your new added file . Write your logic for download the file 
    */
     }
    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) 
     {

    }
    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Hasnain Sabir
  • 200
  • 1
  • 7