Need Objective-C translation of DispatchSource.makeFileSystemObjectSource

I came across a useful repo on GitHub:

https://github.com/GianniCarlo/DirectoryWatcher/blob/master/Sources/DirectoryWatcher/DirectoryWatcher.swift

        self.queue = DispatchQueue.global()
        self.source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: .write, queue: self.queue)
        
        self.source?.setEventHandler {
            [weak self] in
            self?.directoryDidChange()
        }
        
        self.source?.setCancelHandler() {
            close(descriptor)
        }
        
        self.source?.resume()

How do I translate this to OC version? I have an app that was written in OC and I plan to incorporate this directory watcher into the project.

Answered by julio f. in 795030022

self.queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, descriptor, DISPATCH_VNODE_WRITE, self.queue);

__weak typeof(self) weakSelf = self; dispatch_source_set_event_handler(self.source, ^{ [weakSelf directoryDidChange]; });

dispatch_source_set_cancel_handler(self.source, ^{ close(descriptor); });

dispatch_resume(self.source);

Accepted Answer

self.queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, descriptor, DISPATCH_VNODE_WRITE, self.queue);

__weak typeof(self) weakSelf = self; dispatch_source_set_event_handler(self.source, ^{ [weakSelf directoryDidChange]; });

dispatch_source_set_cancel_handler(self.source, ^{ close(descriptor); });

dispatch_resume(self.source);

julio f.’s code looks good, but the original code you got is using a Dispatch global concurrent queue, which is something I specifically recommend against. See Avoid Dispatch Global Concurrent Queues.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Need Objective-C translation of DispatchSource.makeFileSystemObjectSource
 
 
Q