如何快速监视文件夹中的新文件? [英] How to monitor a folder for new files in swift?

查看:174
本文介绍了如何快速监视文件夹中的新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何快速监视文件夹中的新文件,而不会进行轮询(这是非常低效的)?我听说过kqueue和FSEvents等API-但是我不确定是否可以迅速实现它们?

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?

推荐答案

GCD似乎是解决之道. NSFilePresenter类无法正常工作.它们有故障,破损,并且苹果在过去的四年中一直不愿意对其进行修复.可能不推荐使用.

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.

使用GCD处理文件系统事件" ,由 David Hamrick 撰写.

从网站引用的示例代码.我将他的C代码翻译成Swift.

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:

  • Grand Central Dispatch (GCD) dispatch source flags
  • Monitoring a directory in Cocoa/Cocoa Touch

如果您有兴趣观看目录,这是另一篇介绍它的文章.

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

使用GCD监视文件夹" Cocoanetics 上. (很遗憾,我找不到作者的名字.对不起署名,我感到抱歉)

"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)


更新

以前我声称FSEvents API无法正常工作,但是我错了.该API运作良好,如果您有兴趣观看深层文件树,那么它比GCD的简单性会更好.


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.

无论如何,FSEvents不能在纯Swift程序中使用.因为它需要传递C回调函数,所以Swift目前不支持它(Xcode 6.1.1).然后,我不得不退回到Objective-C并重新包装它.

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.

此外,任何此类API都是完全异步的.这意味着在您收到通知时,实际文件系统状态可能会有所不同.那么准确或准确的通知并没有真正的帮助,仅对标记脏标志有用.

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.

我最终为Swift编写了FSEvents的包装. 这是我的工作,希望对您有所帮助.

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

这篇关于如何快速监视文件夹中的新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆