AFNetworking fails to download large files

I’m currently trying to download some files with AFNetworking, for relative small files this seems to work, but I’m trying a slighter larger file (17MB) and it seems to just crash without any error.

The url is linking to a local file: http://test.local/wp-content/uploads/2012/07/test.pdf (I’m running it in the simulator, so this is accessible)

  • modifying a plist is not working
  • How to link to apps on the app store
  • What's the best way to ensure a UITableView reloads atomically?
  • How to get a Core Data Model Picture to fit to 1 page when printing?
  • Swift accessing and updating tableview in container view
  • Change button title color in UIAlertView
  • The only output I get is in the progress block

    progress: 0.009022

    When I check the filesystem, it appears that the file is there, but only a few kb.

    Is this a known error with AFNetworking, or maybe I’m just doing something wrong.

    - (void)downloadIssue:(Issue *)issue
    {
        NSString *fileName = [issue.pdf lastPathComponent];
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
        NSURL *url = [NSURL URLWithString:issue.pdf];
        AFHTTPClient *httpClient = [[[AFHTTPClient alloc] initWithBaseURL:url] autorelease];
        NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:issue.pdf parameters:nil];
    
        AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"PDF DOWNLOAD COMPLETE");
    
            issue.pdf_location = filePath;
    
            // send out a notification with the issue
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_COMPLETE" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"PDF DOWNLOAD FAILED");
    
        // send out a notification with the issue
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_FAILED" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
        }];
    
        [operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            float progress = (float)totalBytesRead / totalBytesExpectedToRead;
    
            NSLog(@"progress: %f", progress);
    
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_PROGRESS" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: issue, @"issue", progress, @"progress", nil]];
        }];
    
        [_queue addOperation:operation];
    }
    

    Solutions Collect From Internet About “AFNetworking fails to download large files”

    I previously had a similar experience as you problem. Try to resolve this issue, it took over almost two weeks.

    The same problem was with you. So I’m glad to finally meet you 🙂

    Here is Solution.

    if in downloadProgress block directly update UI, sometimes occur unknown error. because Ambiguous, but i think Main Thread crash. so, downloadProgress Block only update variable, execute timer update UI using a variable.

    //At the same time download
    myTimer = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:processTimer forMode:NSRunLoopCommonModes];
    
    [operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
    {
        //global ivar
        _progress = (float)totalBytesRead / totalBytesExpectedToRead;
    }];
    
    - (void)updateProgress:(NSTimer *)timer
    {
        myProgressView.progress = _progress;
        if(fabs(1.f-_progress)<0.01f)
        {
            [timer invalidate];
        }
    }