FileExistsAtPath returns NO for the directory that exists

fileExistsAtPathreturns NO for an existing directory. If I have the following code:

NSURL *newDirectoryPath = ... // path does not begin with a tilda
                              // and is created using URLByAppendingPathComponent:isDirectory:

BOOL directoryExists = [self.fileManager fileExistsAtPath:[newDirectoryPath absoluteString]];
if (NO == directoryExists)
{
    BOOL ret = [self.fileManager moveItemAtURL:self.currentPresentationDirectoryPath toURL:newDirectoryPath error: &err];
    // ret is YES, err is nil

    directoryExists = [self.fileManager fileExistsAtPath:[newDirectoryPath absoluteString]];
}

Although the directory has just been successfully created with moveItemAtURL, it fileExistsAtPathstill returns NO.

I know the documentation says the following:

Attempting predicate behavior based on the current state of the file system or a specific file in the file system is not recommended. This can lead to odd behavior in the case of a race condition file.

, : , fileExistsAtPath , , Organizer, .. ..

P.S. fileExistsAtURL: ?

+5
1

NSURL, -absoluteURL NSFileManager. URL file://. : file:///path/to/file.

, -path. , .

NSURL *myURL = /* some url */;
NSString *myPath;
BOOL exi;

myPath = [myURL path];
exi = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if(!exi) {
    NSLog(@"File does not exist");
}
+19

All Articles