You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Observing changes in a file with DispatchSource
The final “lesser known” feature of GCD that I want to bring up is how it provides a way to observe changes in a file on the file system. Like DispatchSemaphore, this is something which can be super useful in a script or command line tool, if we want to automatically react to a file being edited by the user. This enables us to easily build developer tools that have “live editing” features.
Dispatch sources come in a few different variants, depending on what we want to observe. In this case we’ll use DispatchSourceFileSystemObject, which lets us observe events from the file system.
Let's take a look at an example implementation of a simple FileObserver, that lets us attach a closure to be run every time a given file is changed. It works by creating a dispatch source using a fileDescriptor and a DispatchQueue to perform the observation on, and uses Files to refer to the file to observe:
classFileObserver{privateletfile:Fileprivateletqueue:DispatchQueueprivatevarsource:DispatchSourceFileSystemObject?init(file:File){self.file = file
self.queue =DispatchQueue(label:"com.myapp.fileObserving")}func start(closure:@escaping()->Void){
// We can only convert an NSString into a file system representation
letpath=(file.path asNSString)letfileSystemRepresentation= path.fileSystemRepresentation
// Obtain a descriptor from the file system
letfileDescriptor=open(fileSystemRepresentation, O_EVTONLY)
// Create our dispatch source
letsource=DispatchSource.makeFileSystemObjectSource(
fileDescriptor: fileDescriptor,
eventMask:.write,
queue: queue
)
// Assign the closure to it, and resume it to start observing
source.setEventHandler(handler: closure)
source.resume()self.source = source
}}
We can now use FileObserver like this:letobserver=tryFileObserver(file: file)
observer.start{print("File was changed")}
Observing changes in a file with DispatchSource
The final “lesser known” feature of GCD that I want to bring up is how it provides a way to observe changes in a file on the file system. Like DispatchSemaphore, this is something which can be super useful in a script or command line tool, if we want to automatically react to a file being edited by the user. This enables us to easily build developer tools that have “live editing” features.
Dispatch sources come in a few different variants, depending on what we want to observe. In this case we’ll use DispatchSourceFileSystemObject, which lets us observe events from the file system.
Let's take a look at an example implementation of a simple FileObserver, that lets us attach a closure to be run every time a given file is changed. It works by creating a dispatch source using a fileDescriptor and a DispatchQueue to perform the observation on, and uses Files to refer to the file to observe:
ref: https://www.swiftbysundell.com/articles/a-deep-dive-into-grand-central-dispatch-in-swift/
The text was updated successfully, but these errors were encountered: