iOS监控/监测/监听文件/文件夹的变化 检测文件变化

ruje7454 7年前
   <p>我们有些时候,需要监测一个文件/文件夹的变化,例如在某个文件被修改的时候,可以获取到通知,或者我们有个播放列表是扫描某个文件夹下的所有文件,那么当这个目录新添或者删除一些文件后,我们的播放列表要同步更新,这种情况下,我们都需要监听文件/文件夹的变化</p>    <p>碰到这样的一个需求,首先你的反应肯定是,这还不简单,我直接开一个定时器,每隔5s就重新扫描一下指定的文件夹/文件,这不就结了..... 其实这样也没有错,只是多开了一个定时器,效率低一些而已.但还是解决了问题的,对这种方式,假如文件夹中文件很多,效率就更低了,每次重新拿到列表后,还得做字符串的匹配等等...,这种方式,我就不多说了,任何一个开发者分分钟都可以写出这个低效的代码.</p>    <p>这是一个常见的需求,官方其实提供了两种思路来解决这个问题</p>    <h3><strong>方法一</strong></h3>    <p>官方给出的示例demo Classes_DirectoryWatcher 大体上的思路是:</p>    <p>1. 根据文件/文件夹的路径,调用open函数打开文件夹,得到文件句柄。</p>    <p>2. 通过kqueue()函数创建一个kqueue队列来处理系统事件(文件创建或者删除),得到queueId</p>    <p>3. 创建一个kevent结构体,设置相关属性,连同kqueue的ID一起传给kevent()函数,完成系统对kevent的关联。</p>    <p>4. 调用CFFileDescriptorCreateRunloopSouce创建一个接收系统事件的runloop source,同时设置文件描述符的回调函数(回调函数采用C语言标准的回调函数格式), 并加到默认的runloopMode中。</p>    <p>5. 启用回调函数。</p>    <p>6. 关闭kqueue,关闭文件夹</p>    <p>这样操作后,当文件/文件夹有变化,系统会触发相应的回调,你收到回调了,就可以进行各种处理了</p>    <p>其核心代码如下:</p>    <pre>  <code class="language-objectivec">- (void)kqueueFired  {    int             kq;    struct kevent   event;    struct timespec timeout = { 0, 0 };    int             eventCount;      kq = CFFileDescriptorGetNativeDescriptor(self->kqref);    assert(kq >= 0);      eventCount = kevent(kq, NULL, 0, &event, 1, &timeout);    assert( (eventCount >= 0) && (eventCount < 2) );      if (self.fileChangeBlock) {      self.fileChangeBlock(eventCount);    }      CFFileDescriptorEnableCallBacks(self->kqref, kCFFileDescriptorReadCallBack);  }    static void KQCallback(CFFileDescriptorRef kqRef, CFOptionFlags callBackTypes, void *info)    {    MonitorFileChangeUtils *helper = (MonitorFileChangeUtils *)(__bridge id)(CFTypeRef) info;    [helper kqueueFired];  }    - (void) beginGeneratingDocumentNotificationsInPath: (NSString *) docPath  {    int                     dirFD;    int                     kq;    int                     retVal;    struct kevent           eventToAdd;    CFFileDescriptorContext context = { 0, (void *)(__bridge CFTypeRef) self, NULL, NULL, NULL };      dirFD = open([docPath fileSystemRepresentation], O_EVTONLY);    assert(dirFD >= 0);      kq = kqueue();    assert(kq >= 0);      eventToAdd.ident  = dirFD;    eventToAdd.filter = EVFILT_VNODE;    eventToAdd.flags  = EV_ADD | EV_CLEAR;    eventToAdd.fflags = NOTE_WRITE;    eventToAdd.data   = 0;    eventToAdd.udata  = NULL;      retVal = kevent(kq, &eventToAdd, 1, NULL, 0, NULL);    assert(retVal == 0);      self->kqref = CFFileDescriptorCreate(NULL, kq, true, KQCallback, &context);    rls = CFFileDescriptorCreateRunLoopSource(NULL, self->kqref, 0);    assert(rls != NULL);      CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);    CFRelease(rls);    CFFileDescriptorEnableCallBacks(self->kqref, kCFFileDescriptorReadCallBack);  }</code></pre>    <h3><strong>方法二:GCD方式</strong></h3>    <p>大体思路是: 首先是使用O <em>EVTONLY模式打开文件/文件夹,然后创建一个GCD的source,当然了,其unsigned long mask参数要选VNODE系列的,例如要检测文件的些人,可以用DISPATCH</em> VNODE_WRITE</p>    <p>核心代码:</p>    <pre>  <code class="language-objectivec">- (void)__beginMonitoringFile  {      _fileDescriptor = open([[_fileURL path] fileSystemRepresentation],                           O_EVTONLY);    dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    _source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,                                     _fileDescriptor,                                     DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_DELETE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE | DISPATCH_VNODE_WRITE,                                     defaultQueue);            dispatch_source_set_event_handler(_source, ^ {      unsigned long eventTypes = dispatch_source_get_data(_source);      [self __alertDelegateOfEvents:eventTypes];    });      dispatch_source_set_cancel_handler(_source, ^{      close(_fileDescriptor);      _fileDescriptor = 0;      _source = nil;        // If this dispatch source was canceled because of a rename or delete notification, recreate it      if (_keepMonitoringFile) {          _keepMonitoringFile = NO;          [self __beginMonitoringFile];      }    });    dispatch_resume(_source);  }</code></pre>    <h3><strong>参考文档</strong></h3>    <p><a href="/misc/goto?guid=4959725612109974263" rel="nofollow,noindex">handling-filesystem-events-with-gcd</a> <a href="/misc/goto?guid=4959725612195914544" rel="nofollow,noindex">Monitoring-Files-With-GCD-Being-Edited-With-A-Text-Editor</a> <a href="/misc/goto?guid=4959725612274943353" rel="nofollow,noindex">gcd-zhi-jian-ting-wen-jian</a> <a href="/misc/goto?guid=4959725612349964787" rel="nofollow,noindex">GCDWorkQueues</a> <a href="/misc/goto?guid=4959725612440599607" rel="nofollow,noindex">iMonitorMyFiles</a></p>    <p> </p>    <p> </p>