在继续[Obj-C]之前检查文件是否存在

时间:2011-11-05 02:23:34

标签: objective-c filesystems while-loop nsfilemanager

目前这是我的代码

NSFileManager *fileManager = [[NSFileManager alloc] init];

BOOL receiptExists = NO;
BOOL didLog = NO;

while (!receiptExists) {

    receiptExists = [fileManager fileExistsAtPath:PATH];

    if (!didLog) {
        NSLog(@"[NOTICE]: Waiting for the file to appear...\n");
        didLog = YES;
    }
}
// rest of the code

这个while循环消耗了大量资源,我确信有更好的obj-c实现。有什么想法?

1 个答案:

答案 0 :(得分:5)

您可以使用kqueue / kevent系统或FSEvents。

使用此功能,您可以收到更改通知,而不是轮询它们。

如果你想要这些的Objective-C抽象,你可能想尝试UKKQueueSCEvent