Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sporadic Cannot Read file Error - File does not exist

I am sporadically getting an error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot read file at file:///var/mobile/Applications/D743821C-6F34-4E97-8FBA-D7EAD6738E38/Documents/contacts.zip

*** First throw call stack: (0x2ef1ef83 0x39799ccf 0x2eb5f7c3 0x2eb5f2dd 0x39c8181f 0x39c87677 0x2eb5f289 0x2eb9897f 0x172cb9 0x17bac3 0x5b769 0x160d99 0x69f2d 0x698cb 0x1709c7 0x17a9a3 0x39c81833 0x39c88ad7 0x39c88d29 0x39dc3bd3 0x39dc3a98) libc++abi.dylib: terminating with uncaught exception of type NSException

The program falls over at line

Info.uploadTask = [self.session uploadTaskWithRequest:request fromFile:url];

When I look on in the device container I see the file is not there.

I do not understand

  1. why the exists at path check passes if the file does not exist
  2. why the program fails even though it is in a try catch block

    NSString *path = file.myPath;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        @try {
            NSURL *url = [NSURL fileURLWithPath:path];
            Info.uploadTask = [self.session uploadTaskWithRequest:request fromFile:url];
        }
        @catch (NSException *exception) {
            BLog("error Reading file:%@",path);
            return;
        }
    } else {
        BLog(@"file not found:%@",path);
        return;
    }
    
like image 985
Ryan Heitner Avatar asked Nov 10 '22 23:11

Ryan Heitner


1 Answers

So I had the exactly same issue - NSURLSession couldn't read the file for some reason. However, I am able to catch an exception using the same code that you have. I created a category for convenience:

@implementation NSURLSession (DMAdditions)
- (NSURLSessionUploadTask * _Nullable)crashFreeUploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL {
  @try {
    return [self uploadTaskWithRequest:request fromFile:fileURL];
  }
  @catch (NSException *exception) {
    NSLog(@"We crashed: %@", exception);
    return nil;
  }
}
@end
like image 124
almas Avatar answered Nov 14 '22 23:11

almas