gpt4 book ai didi

objective-c - 如何在 OS X 上查看文件更改?

转载 作者:太空狗 更新时间:2023-10-30 03:57:53 24 4
gpt4 key购买 nike

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

最佳答案

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

@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

关于objective-c - 如何在 OS X 上查看文件更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com