【问题标题】:Checking if a file exists before continuing [Obj-C]在继续 [Obj-C] 之前检查文件是否存在
【发布时间】:2011-12-22 11:55:23
【问题描述】:

目前这里是我的代码

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 实现。有什么想法吗?

【问题讨论】:

    标签: objective-c filesystems while-loop nsfilemanager


    【解决方案1】:

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

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

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

    【讨论】:

    • 其实FSEvents和kqueue是分开的,有自己的内核支持。
    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 2012-07-02
    • 2021-10-16
    • 2021-12-25
    • 1970-01-01
    • 2012-02-27
    • 2019-03-07
    相关资源
    最近更新 更多