我不是從那裏我得到這個代碼肯定的,但我敢肯定,我從某處計算器只複製該代碼。此代碼監視文件,當它被監視的文件已變更的情況下,如果()條件內的語句。
-(void)monitorFile:(NSString*) path
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
int fildes = open([path UTF8String], O_EVTONLY);
__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);
if(flags)
{
// Do Stuff here
[blockSelf monitorFile:path];
}
});
dispatch_source_set_cancel_handler(source, ^(void) {
close(fildes);
});
dispatch_resume(source);
}
編輯:我發現從我抄自原答案:https://stackoverflow.com/a/26304208/7433869