Search code examples
objective-cfilesystemswhile-loopnsfilemanager

Checking if a file exists before continuing [Obj-C]


currently here is my code

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

This while loop consumes a lot of resources and I'm sure there is a better obj-c implementation. Any thought ?


Solution

  • You can use the kqueue/kevent system, or FSEvents.

    Using this, you can be notified of changes, instead of polling for them.

    If you'd like an Objective-C abstraction of these, you may want to try UKKQueue or SCEvent.