Reputation: 361
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?
Upvotes: 0
Views: 515
Reputation: 2043
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);
}
}
};
}
Upvotes: 2