Working with file system on macOS. Part 3
Finally, I have came around to shipping a solution for monitoring file system events using higher-level API. The main advantage is potentially to be able to ship to iOS and other Apple platforms. I couldn’t do it myself without the help from mighty the internet.
Well-documented example code for writing a wrapper around DispatchSource
for monitoring changes in a single file → https://swiftrocks.com/dispatchsource-detecting-changes-in-files-and-folders-in-swift
A nice article which describes how to cancel DispatchSource
connection → https://agostini.tech/2017/08/06/monitoring-files-using-dispatch-sources/
Sample Swift code from Apple Engineer https://forums.developer.apple.com/forums/thread/90531 demonstrating how to monitor changes in a directory. It also features a debounce pattern which can help with performance.
Implementation problems
I made changes to LinkEdit to allow the creating new NSDocument
subclasses, my initial implementation caused these 2 warnings.
Trying to save a document without any appropriate writable type defined.
And the second one.
The app did not specify supported document types in its Info.plist. This is discouraged.
These two warnings were fixed with the following code in my NSDocument
subclass.
override class var writableTypes: [String] {
return ["net.daringfireball.markdown"]
}
Useful links
- Useful article Markdown Playgrounds: Setting Up a Document-Based App
- Good introduction to
DispatchSource
from agostini.tech.
Real application
This post wouldn’t be possible without the work which went into LinkEdit. It describes one of the many features and improvements included in recent 1.3 major update.