简体   繁体   中英

NSFileManager not deleting a file that exists

I am having some problem with using the NSFileManager functionalities. This happens both on the Simulator and an iPhone device (iOS 5.1).

Basically, I have a bunch of files that are stored in a document that I created. Now I am trying to move a file (stored at path) to the same directory with another name, to check if the deletion works.

if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:path toPath:[path            stringByAppendingString:@".deleted"] error:&error];
if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}

The output of this is both files at path and path .deleted. I ultimately just want to remove the file using removeItemAtPath but that is not working. It returns a success but if I see it in the file directory I can still see it there even after an hour.

If you want to delete a file you should use removeItemAtPath:myPath error:NULL like

NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}

Use this for search path and delete a video

(NSString *)applicationDocumentsDirectory {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
    }
NSString *dataFilePath = [[[self applicationDocumentsDirectory]
                               stringByAppendingPathComponent:@"Data.nosync"]
                              stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@.mp4",[dicForDelete valueForKey:keyVideoId],[dicForDelete valueForKey:keyVideoName]]];
NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:dataFilePath]) {
        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:dataFilePath error:&error];
        if (success) {
          NSLog@"file deleted ");
        }else
        {
        NSLog@"file not deleted ");
        }
    }   

I ran into the same issue. I'm still baffled as to why the fix works, but it's worth mentioning in case others can explain it.

I discovered that [[NSFileManager defaultManager] removeItemAtURL:url error:&error] would return false on the first attempt but with a nil error. On the second attempt it would return true.

Here's what my code looked like:

// success == NO error = nil
BOOL success = [[NSFileManager defaultManager] removeItemAtURL:storeUrl error:&error]; 

// success == YES error = nil
success = [[NSFileManager defaultManager] removeItemAtURL:storeUrl error:&error];

I discovered that if I created my own NSFileManager instance, it would work on the first try.

NSFileManager *fm = [[NSFileManager alloc] init];
[fm removeItemAtURL:storeUrl error:&error];

I'm running on the iOS9 simulator. This smells like a bug. I'm going to file a radar.

I just figure it out of something very important when you use NSFileManager. You have to be aware of App Sandboxing.

let documentDirectory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]

This line return the path document directory of your app sandboxed. When you create a file with FileManager in your document directory (for eg) don't save the full file path but only the path from the current document directory.

You'll be able to recreate the full path of your created file.

Hope (after 5 years) help over developers ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM