Reputation: 8070
What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added?
Upvotes: 22
Views: 9734
Reputation: 7734
Nowadays you can use GCD (Grand Central Dispatch) to monitor folders. You might be able to use the same technique for monitoring a file, but even if it only works on folders you can note the modification date of the file and check for changes each time the folder changes.
Here's a Swift class I wrote to monitor a folder using GCD:
import Foundation
@objc public class DirectoryWatcher : NSObject {
override public init() {
super.init()
}
deinit {
stop()
}
public typealias Callback = (_ directoryWatcher: DirectoryWatcher) -> Void
@objc public convenience init(withPath path: String, callback: @escaping Callback) {
self.init()
if !watch(path: path, callback: callback) {
assert(false)
}
}
private var dirFD : Int32 = -1 {
didSet {
if oldValue != -1 {
close(oldValue)
}
}
}
private var dispatchSource : DispatchSourceFileSystemObject?
@objc public func watch(path: String, callback: @escaping Callback) -> Bool {
// Open the directory
dirFD = open(path, O_EVTONLY)
if dirFD < 0 {
return false
}
// Create and configure a DispatchSource to monitor it
let dispatchSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: dirFD, eventMask: .write, queue: DispatchQueue.main)
dispatchSource.setEventHandler {[unowned self] in
callback(self)
}
dispatchSource.setCancelHandler {[unowned self] in
self.dirFD = -1
}
self.dispatchSource = dispatchSource
// Start monitoring
dispatchSource.resume()
// Success
return true
}
@objc public func stop() {
// Leave if not monitoring
guard let dispatchSource = dispatchSource else {
return
}
// Don't listen to more events
dispatchSource.setEventHandler(handler: nil)
// Cancel the source (this will also close the directory)
dispatchSource.cancel()
self.dispatchSource = nil
}
}
Use it like Apple's DirectoryWatcher example, something like this:
let directoryWatcher = DirectoryWatcher(withPath: "/path/to/the/folder/you/want/to/monitor/", callback: {
print("the folder changed")
})
Destroying the object will stop watching, or you can stop it explicitly
directoryWatcher.stop()
It should be compatible with Objective C they way it's written (untested). Using it would be like this:
DirectoryWatcher *directoryWatcher = [DirectoryWatcher.alloc initWithPath: @"/path/to/the/folder/you/want/to/monitor/" callback: ^(DirectoryWatcher *directoryWatcher) {
NSLog(@"the folder changed")
}];
Stopping it is similar
[directoryWatcher stop];
Upvotes: 2
Reputation: 69312
If you do need to use kqueue (as discussed in other answers) Google Toolbox for Mac has a nice Objective-C wrapper that I've used with no issues thus far.
Upvotes: 2
Reputation: 46020
The FSEvents API is ideal if you just want to watch directories but it doesn't handle the monitoring of individual files. Stu Connolly has a great Objective-C wrapper for the FSEvents C API, it's called SCEvents and you can get it here:
http://stuconnolly.com/blog/scevents-011/
The nice thing about FSEvents is that you just need to watch one folder and you will be notified of any changes that occur anywhere in the subfolder hierarchy of that folder.
If you need file-level notifications you will need to use kqueues. Uli Kusterer has a great Objective-C wrapper:
http://zathras.de/angelweb/sourcecode.htm#UKKQueue
Either of these methods is a lot easier than wrangling with the C APIs directly, which are not particularly well documented and a bit obtuse.
If you need to support Tiger you'll need to use kqueues as the FSEvents API wasn't officially available in 10.4.
Upvotes: 33
Reputation: 96967
If you are changing a file or folder, I believe the Spotlight search engine will update its database to reflect your changes.
So you might set up a thread that listens for kMDQueryDidUpdateNotification notifications through a Spotlight query specific to that file or folder.
When you get those notifications, you could fire a selector that does something you want.
Upvotes: 1
Reputation: 8810
Not sure what's the best way, but A way would be to fire up an NSThread that would regularly (for instance every second) check the creation dates of the files in the directory, and then have a delegate associated with that thread to perform some action when a new file has been added
Upvotes: -3