0

As title, I am handling a case that when downloading a large zip file using AFNetworking 2.5.4 framework, the user quit the app accidentally. I would like to know if it is necessary to manually clear the Incomplete folder (<app_name>/tmp/Incomplete) generated by AFNetworking, as the zip file downloading is quite large. Will iOS handle this folder if it find the folder size if growing?

P.S. My downloading logic is by using NSOperationQueue and the below AFDownloadRequestOperation:

- (AFDownloadRequestOperation *)operation
{
    if (_operation) {
        return _operation;
    }
    _operation = [[AFDownloadRequestOperation alloc] initWithRequest:self.downloadRequest
                                                          targetPath:self.targetPath
                                                        shouldResume:YES];
#ifdef DEBUG
    _operation.securityPolicy.allowInvalidCertificates = YES;
#endif
    [_operation setProgressiveDownloadProgressBlock:self.progressBlock];
    [_operation setCompletionBlockWithSuccess:self.completionBlock
                                      failure:self.failureBlock];
    _operation.deleteTempFileOnCancel = YES;

    return _operation;
}
chubao
  • 5,871
  • 6
  • 39
  • 64

1 Answers1

0

you can resume the incomplete download with this

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"];
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
        NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
        NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
        NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
        NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
        NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
    }];
    [operations addObject:operation];

OR you can delete temp files

    + (void)clearTmpDirectory
{
    NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in tmpDirectory) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
}
viratpuar
  • 524
  • 1
  • 5
  • 22
  • Is that mean I need to handle it manually? That is when the downloading find that there is not enough space in the device, will iOS clear up the `tmp` folder? – chubao Feb 16 '16 at 09:07
  • 2
    i think If you can't delete files then iphone handle it. **automatically deletes cached files when it needs more space** refer this [link](https://support.apple.com/en-us/HT201656) – viratpuar Feb 16 '16 at 09:15
  • From the previous [link](https://support.apple.com/en-us/HT201656), _If you want to delete the cached files from your iOS device Your iOS device automatically deletes cached files when it needs more space. You don't need to delete them yourself._ Can anyone provide more details to this? – chubao Feb 16 '16 at 09:17