3
votes

I create a directory in the viewDidLoad at the Documents folder of my app.

NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.projectsLocalPath = [localDirectory stringByAppendingPathComponent:@"Projects"];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.projectsLocalPath] == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:self.projectsLocalPath withIntermediateDirectories:YES attributes:nil error:nil];
}

After that, I list all files so that they appear in logs, checking wether it is a directory or not:

//----- LIST ALL FILES -----
NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:localDirectory error:nil];
for (NSString *aPath in contentOfFolder) {
    NSString *fullPath = [localDirectory stringByAppendingPathComponent:aPath];
    
    BOOL isDir;
    if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) {
        NSLog(@"%@ - IT IS A DIR", fullPath);
    }
    else {
        NSLog(@"%@ - IT IS NOT A DIR", fullPath);
    }
}

When I first run the app, the log files list look like this:

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-shm - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-wal - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Projects - IT IS A DIR

Everything is correct. But the following runs show in the logs that the Projects folder is not a directory anymore.

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-shm - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-wal - IT IS NOT A DIR

/var/mobile/Containers/Data/Application/E3...94/Documents/Projects - IT IS NOT A DIR

It is like it converts magically into a file... The problem is that when I want to see the directory's contents, I'm getting an error saying that it is NOT a directory.

Am I doing anything wrong? This problem is driving me nuts...

Thank you all for your help in advance.

EDIT:

At the first run, once directory is created, I download some files and save them into the newly created directory using the dropbox API: [self.restClient loadFile:plan.path intoPath:self.projectsLocalPath];. There is no error when saving files into this directory.

1
What happens between the first and the second run? Are they independent? The result is randomly? - Amin Negm-Awad
@AminNegm-Awad At the first run, once directory is created, I download some files and save them into the newly created directory using the dropbox API: [self.restClient loadFile:plan.path intoPath:self.projectsLocalPath];. There is no error when saving files into this directory. - anonymous
I created a sample project copied all your code in my viewDidLoad. I'm not getting any issues, everytime that shows as a directory in my Log !!! The directory GUID will be different in each run, but in your case that is similar, why is that ? Are you hardcoding that ? - Midhun MP
@KrLx_roller And when do you start the second run? Immediately after downloading the files? - Amin Negm-Awad
@AminNegm-Awad I wait for the three files to be downloaded and saved. Then, I rerun the app and the directory it seems to be now a file. - anonymous

1 Answers

4
votes

By calling:

[self.restClient loadFile:plan.path intoPath:self.projectsLocalPath];

you're asking the rest client to place the file at the folder location - replacing the folder - not placing it in the folder.

You need to create a full path to the destination file, including the file name inside the folder.