I need to monitor a file for when its metadata changes.
I'm using the following code I found.
The problem is that I am getting nearly 7,000 notifications every time a change is detected, which is causing the CPU to get rather warm.
This subject seems to be very poorly documented and after days searching, I don't seem to be able to make things any better.
Any ideas please?
Joanna
I'm using the following code I found.
Code Block final class FileMonitor { let url: URL var fileHandle: FileHandle? var source: DispatchSourceFileSystemObject? var didReceiveChanges: (() -> Void)? func process(event: DispatchSource.FileSystemEvent) { print(String(describing: event)) didReceiveChanges?() } func eventHandler() { let event = self.source!.data process(event: event) createDispatchSource() } func createDispatchSource() { self.fileHandle = try? FileHandle(forReadingFrom: url) source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: fileHandle!.fileDescriptor, eventMask: [.attrib], queue: DispatchQueue.main) source!.setEventHandler(handler: eventHandler) source!.setCancelHandler { self.fileHandle!.closeFile() } source!.resume() } init(url: URL) throws { self.url = url createDispatchSource() } deinit { source!.cancel() } }
The problem is that I am getting nearly 7,000 notifications every time a change is detected, which is causing the CPU to get rather warm.
This subject seems to be very poorly documented and after days searching, I don't seem to be able to make things any better.
Any ideas please?
Joanna