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

b1o*_*nic 3 filesystems objective-c 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
Run Code Online (Sandbox Code Playgroud)

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

jus*_*tin 5

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

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

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