How to monitor a folder for new files in swift?

2020-05-14 08:21发布

How would I monitor a folder for new files in swift, without polling (which is very inefficient)? I've heard of APIs such as kqueue and FSEvents - but I'm not sure it's possible to implement them in swift?

标签: macos swift
9条回答
疯言疯语
2楼-- · 2020-05-14 08:35

I've tried to go with these few lines. So far seems to work.

class DirectoryObserver {

    deinit {

        dispatch_source_cancel(source)
        close(fileDescriptor)
    }

    init(URL: NSURL, block: dispatch_block_t) {

        fileDescriptor = open(URL.path!, O_EVTONLY)
        source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(fileDescriptor), DISPATCH_VNODE_WRITE, dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT))
        dispatch_source_set_event_handler(source, { dispatch_async(dispatch_get_main_queue(), block) })
        dispatch_resume(source)
    }

    //

    private let fileDescriptor: CInt
    private let source: dispatch_source_t
}

Be sure to not get into retain cycle. If you are going to use owner of this instance in block, do it safely. For example:

self.directoryObserver = DirectoryObserver(URL: URL, block: { [weak self] in

    self?.doSomething()
})
查看更多
虎瘦雄心在
3楼-- · 2020-05-14 08:36

Depending on your application needs, you may be able to use a simple solution.

I actually used kqueue in a production product; I wasn't crazy with the performance but it worked, so I didn't think too much of it till I found a nice little trick that worked even better for my needs, plus, it used less resources which can be important for performance intensive programs.

What you can do, again, if your project permits, is that every time you switch to your application, you can just check the folder as part of your logic, instead of having to periodically check the folder using kqueue. This works and uses far less resources.

查看更多
smile是对你的礼貌
4楼-- · 2020-05-14 08:37

Swift 5 Version for Directory Monitor, with GCD, original from Apple

import Foundation

/// A protocol that allows delegates of `DirectoryMonitor` to respond to changes in a directory.
protocol DirectoryMonitorDelegate: class {
    func directoryMonitorDidObserveChange(directoryMonitor: DirectoryMonitor)
}

class DirectoryMonitor {
    // MARK: Properties

    /// The `DirectoryMonitor`'s delegate who is responsible for responding to `DirectoryMonitor` updates.
    weak var delegate: DirectoryMonitorDelegate?

    /// A file descriptor for the monitored directory.
    var monitoredDirectoryFileDescriptor: CInt = -1

    /// A dispatch queue used for sending file changes in the directory.
    let directoryMonitorQueue =  DispatchQueue(label: "directorymonitor", attributes: .concurrent)

    /// A dispatch source to monitor a file descriptor created from the directory.
    var directoryMonitorSource: DispatchSource?

    /// URL for the directory being monitored.
    var url: URL

    // MARK: Initializers
    init(url: URL) {
        self.url = url
    }

    // MARK: Monitoring

    func startMonitoring() {
        // Listen for changes to the directory (if we are not already).
        if directoryMonitorSource == nil && monitoredDirectoryFileDescriptor == -1 {
            // Open the directory referenced by URL for monitoring only.
            monitoredDirectoryFileDescriptor = open((url as NSURL).fileSystemRepresentation, O_EVTONLY)

            // Define a dispatch source monitoring the directory for additions, deletions, and renamings.
            directoryMonitorSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: monitoredDirectoryFileDescriptor, eventMask: DispatchSource.FileSystemEvent.write, queue: directoryMonitorQueue) as? DispatchSource

            // Define the block to call when a file change is detected.
            directoryMonitorSource?.setEventHandler{
                // Call out to the `DirectoryMonitorDelegate` so that it can react appropriately to the change.
                self.delegate?.directoryMonitorDidObserveChange(directoryMonitor: self)
            }

            // Define a cancel handler to ensure the directory is closed when the source is cancelled.
            directoryMonitorSource?.setCancelHandler{
                close(self.monitoredDirectoryFileDescriptor)

                self.monitoredDirectoryFileDescriptor = -1

                self.directoryMonitorSource = nil
            }

            // Start monitoring the directory via the source.
            directoryMonitorSource?.resume()
        }
    }

    func stopMonitoring() {
        // Stop listening for changes to the directory, if the source has been created.
        if directoryMonitorSource != nil {
            // Stop monitoring the directory via the source.
            directoryMonitorSource?.cancel()
        }
    }
}

查看更多
▲ chillily
5楼-- · 2020-05-14 08:45

GCD seems to be the way to go. NSFilePresenter classes doesn't work properly. They're buggy, broken, and Apple is haven't willing to fix them for last 4 years. Likely to be deprecated.

Here's a very nice posting which describes essentials of this technique.

"Handling Filesystem Events with GCD", by David Hamrick.

Sample code cited from the website. I translated his C code into Swift.

    let fildes = open("/path/to/config.plist", O_RDONLY)

    let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    let source = dispatch_source_create(
        DISPATCH_SOURCE_TYPE_VNODE,
        UInt(fildes),
        DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
        queue)

    dispatch_source_set_event_handler(source,
        {
            //Reload the config file
        })

    dispatch_source_set_cancel_handler(source,
        {
            //Handle the cancel
        })

    dispatch_resume(source);

    ...

        // sometime later
        dispatch_source_cancel(source);

For reference, here're another QAs posted by the author:


If you're interested in watching directories, here's another posting which describes it.

"Monitoring a Folder with GCD" on Cocoanetics. (unfortunately, I couldn't find the author's name. I am sorry for lacking attribution)

The only noticeable difference is getting a file-descriptor. This makes event-notification-only file descriptor for a directory.

_fileDescriptor = open(path.fileSystemRepresentation(), O_EVTONLY)

Update

Previously I claimed FSEvents API is not working, but I was wrong. The API is working very well, and if you're interested in watching on deep file tree, than it can be better then GCD by its simplicity.

Anyway, FSEvents cannot be used in pure Swift programs. Because it requires passing of C callback function, and Swift does not support it currently (Xcode 6.1.1). Then I had to fallback to Objective-C and wrap it again.

Also, any of this kind API is all fully asynchronous. That means actual file system state can be different at the time you are receiving the notifications. Then precise or accurate notification is not really helpful, and useful only for marking a dirty flag.

Update 2

I finally ended up with writing a wrapper around FSEvents for Swift. Here's my work, and I hope this to be helpful.

查看更多
Bombasti
6楼-- · 2020-05-14 08:45

You could add UKKQueue to your project. See http://zathras.de/angelweb/sourcecode.htm it's easy to use. UKKQueue is written in Objective C, but you can use it from swift

查看更多
祖国的老花朵
7楼-- · 2020-05-14 08:46

SKQueue is a Swift wrapper around kqueue. Here is sample code that watches a directory and notifies of write events.

class SomeClass: SKQueueDelegate {
  func receivedNotification(_ notification: SKQueueNotification, path: String, queue: SKQueue) {
    print("\(notification.toStrings().map { $0.rawValue }) @ \(path)")
  }
}

if let queue = SKQueue() {
  let delegate = SomeClass()

  queue.delegate = delegate
  queue.addPath("/some/file/or/directory")
  queue.addPath("/some/other/file/or/directory")
}
查看更多
登录 后发表回答