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?