0

I have implemented a file observer on a folder observing for the create & delete as follows:

private FileObserver getFileObserverListener(String filepath, String fileType) {
    return new FileObserver(filepath) {
      @Override
      public void onEvent(int i, @Nullable String s) {
        if ((FileObserver.CREATE & i) != 0 || (FileObserver.DELETE & i) != 0) {
          refreshFolderData(filepath, fileType);
        }
      }
    };
  }

But when I am creating/deleting multiple files in a single batch, the file observer is triggered for every single delete/create operation. Is there a way to avoid this? More specifically, is there a way to trigger the file observer exactly once for a batch of create/delete operations in a particular folder?

Adi
  • 361
  • 1
  • 5
  • 23

1 Answers1

2

If you're closing your stream after you finish deleting all files. you can use this event

CLOSE_WRITE

Your code will be like that.

private FileObserver getFileObserverListener(String filepath, String fileType) {
    return new FileObserver(filepath) {
      @Override
      public void onEvent(int i, @Nullable String s) {
        if (event == CLOSE_WRITE) {
          refreshFolderData(filepath, fileType);
        }
      }
    };
  }
Mohamed Nageh
  • 1,963
  • 1
  • 19
  • 27