I wrote a subclass of UITableViewController that lists the files in a directory group along with their respective sizes. It is also updated when there are any changes to these directories. The class uses DispatchSourceto "browse" directories. Here is the code that does this:
for dir in directories {
let fd = dir.withUnsafeFileSystemRepresentation { (filenamePointer) -> Int32 in
return vfw_open(filenamePointer, O_EVTONLY)
}
guard fd != 0 else {
return
}
let watcher = DispatchSource.makeFileSystemObjectSource(fileDescriptor: fd,
eventMask: DispatchSource.FileSystemEvent.write,
queue: DispatchQueue.global(qos: .utility))
watcher.setEventHandler { [weak self] in
DispatchQueue.main.async {
self?.updateFileList()
}
}
watcher.setCancelHandler() {
close(fd)
}
watcher.resume()
}
updateFileList, . , . , , updateFileList . , 0 . , , updateFileList , , . ?