2

Error trying to delete an existing file

 2 years ago
source link: https://www.codesd.com/item/error-trying-to-delete-an-existing-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Error trying to delete an existing file

advertisements

A tester said that he got this message only once, and I can't seem to reproduce it or figure out how this message appeared. In a nut shell, my application starts a background thread and grabs data from our server. Once we are done downloading the data we store it in an xml file, so we can grab the data from this file incase we shutdown durning the parsing of the xml. Once I am done parsing the xml I delete the file. Here is my code for deleting.

-(void)deleteSavedXmlServerData
{
    AppDelegate_Shared* appDelegate = (AppDelegate_Shared*)[[UIApplication sharedApplication] delegate];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileToLoad = [NSString stringWithFormat:@"%@/%@/upload.xml", [paths objectAtIndex:0], appDelegate.userId];

   if([[NSFileManager defaultManager] fileExistsAtPath:fileToLoad] == YES)
   {

        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:fileToLoad error:&error];
        if(error != nil)
        {

             eNSLog(@"%s Error Parsing old data: %@ info: %@", __PRETTY_FUNCTION__, error, [error userInfo]);

             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Problem With Syncing"
                                                        message:@"Problem reading in old data."
                                                       delegate:nil
                                              cancelButtonTitle:@"ok"
                                              otherButtonTitles:nil];
             [alert show];
             [alert release];
        }
    }
}

Everything seems to work perfectly, and it is not a big deal if this file does not get deleted. But I am confused why I would have an issue with deleting this file.

When I checked what the error message was this is what I got this.

 -[ServerSync deleteSavedXmlServerData] Error Parsing old data: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x11f3ed60 "Operation could not be completed. (Cocoa error 4.)" info: {
    NSFilePath = "/var/mobile/Applications/CD181518-512F-4A0E-82ED-C438886E4A1D/Documents/71BD9A11-A604-6001-549C-DF6582F60124/upload.xml";
    NSUserStringVariant = Remove;
}

If anyone know why if my file exist and I created it, why I cannot delete it?

Update

The question that confused me the most, is why does the first if statement say the file exist, but the error message that I get from trying to remove the file says that my file does not exist?


Reading the documentation for NSFileManager (don't know if I just missed it the first time), it says to not use the singleton method [NSFileManager defaultManager] because NSFileManager is not thread safe. I have not had this problem since then.

Tags iphone

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK