5

我希望收到写入给定文件的通知——无需轮询,无需从文件中读取,也无需监视父目录和查看文件修改时间戳。我怎么做?

4

2 回答 2

9

我找不到一个简单的例子,所以我贡献了我想出的东西以供将来参考:

@interface FileWatch ()
@property(assign) dispatch_source_t source;
@end

@implementation FileWatch
@synthesize source;

- (id) initWithPath: (NSString*) path targetQueue: (dispatch_queue_t) queue block: (dispatch_block_t) handler
{
    self = [super init];

    int descriptor = open([path fileSystemRepresentation], O_EVTONLY);
    if (descriptor < 0) {
        return nil;
    }

    [self setSource:dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, descriptor, DISPATCH_VNODE_WRITE, queue)];
    dispatch_source_set_event_handler(source, handler);
    dispatch_source_set_cancel_handler(source, ^{
        close(descriptor);
    });

    dispatch_resume(source);
    return self;
}

- (void) dealloc
{
    if (source) {
        dispatch_source_cancel(source);
        dispatch_release(source);
        source = NULL;
    }
}

@end
于 2012-07-12T08:26:47.887 回答
4

根据我的经验,在某些情况下,文件不仅被写入,而且被删除然后重写(某些 plist 文件的情况)。然后你必须稍微修改一下代码:当文件被替换时再次调用该方法以保持监控。

- (void) myMonitoringMethodWithPath: :(NSString*) path
        __block typeof(self) blockSelf = self;
        __block dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,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, ^
                                      {
                                       unsigned long flags = dispatch_source_get_data(source);
                                          //Do some stuff

                                          if(flags & DISPATCH_VNODE_DELETE)
                                          {
                                              [blockSelf myMonitoringMethodWithPath:path];

                                          }
                                      });
    dispatch_source_set_cancel_handler(source, ^(void) 
                                       {
                                           close(fildes);
                                       });
    dispatch_resume(source);
}
于 2012-07-12T16:25:21.137 回答