0

I am using AFHTTPRequestOperation to download a large file to the Documents directory on my device.

NSURLRequest *request = [NSURLRequest requestWithURL:vectorFile.url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSString *path = [self pathForFileName:vectorFile.fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

    double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
    progress(percentDone, totalBytesRead, totalBytesExpectedToRead);

}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        NSString *path = [self pathForFileName:vectorFile.fileName];

        extracting();
        [SSZipArchive unzipFileAtPath:path toDestination:[self path] progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {

            NSLog(@"Unzipping");

        } completionHandler:^(NSString *path2, BOOL succeeded, NSError *error) {

            if (!error) {
                NSLog(@"Successfully downloaded file to %@", path);


                dispatch_async(dispatch_get_main_queue(), ^{

                    completion(YES);

                });
            } else {

                NSLog(@"%@", error);
                failure(error);

            }


        }];

    });


} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {

    NSLog(@"%@", error);
    failure(error);

}];
[operation start];

For some strange reason, it works on the Simulator to download the files, but on the device (iPhone 6) I get the following error:

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"

The URL is correct, as it downloads in my browser correctly and in the Simulator. Why would this just be happening on the device? And what could be causing it?

I have tried restarting my device and resetting network settings.

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
  • Three common sources of problems between simulator and device include (a) trying to write to bundle; (b) case sensitivity (macOS generally is not case sensitive; iOS is); and (c) trying to access files outside of the sandbox. If you show us what the path was, we might be able to diagnose what's going on. – Rob Jul 27 '16 at 22:11
  • @Rob - You are wanting to know the download path or the path I am writing the file to? – Nic Hubbard Jul 27 '16 at 23:06
  • @Rob - You were right! I made sure to create the directory first before streaming and that fixed the issue! Want to add that as an answer? – Nic Hubbard Jul 27 '16 at 23:26
  • You should feel free to [answer your own question](http://stackoverflow.com/help/self-answer). That's OK w me. – Rob Jul 27 '16 at 23:28
  • I am unable to unfind any solution yet. This issue happens on device but chances are very rare but it happen on simulator when i run the application from xcode without stoping old running applicaiton – Iqbal Khan Oct 13 '17 at 06:03

2 Answers2

3

This error (No such file or directory) can also be thrown if you using [NSURLSessionConfiguration backgroundConfiguration]

In iOS 10 at least, if you try to resume such tasks using whatever resumeData is there, your session delegate call will return that error.

Not sure what has changed since iOS 9, since this was working properly in iOS 7-9.

Aleksandar Vacić
  • 4,433
  • 35
  • 35
1

After help a comment here, I figured out that I needed to check first that my destination directory had been created:

if(![[NSFileManager defaultManager] fileExistsAtPath:[self path]]) {
     [[NSFileManager defaultManager] createDirectoryAtPath:[self path] withIntermediateDirectories:YES attributes:nil error:NULL];
}

I had assumed that the outputStream would have created it for me. And the even strange thing is that it worked correctly in the simulator.

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412