2016-02-16 48 views
0

作爲標題,我正在處理使用AFNetworking 2.5.4框架下載大型壓縮文件時,用戶意外退出應用程序的情況。我想知道是否有必要手動清除AFNetworking生成的Incomplete文件夾(<app_name>/tmp/Incomplete),因爲zip文件的下載量非常大。如果iOS發現文件夾大小增加,它會處理這個文件夾嗎?是否需要手動清除AFNetworking生成的Incomplete文件夾?

P.S.我下載的邏輯是通過使用NSOperationQueue及以下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; 
} 

回答

0

您可以恢復不完整下載這個

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可以刪除臨時文件

+ (void)clearTmpDirectory 
{ 
    NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]; 
    for (NSString *file in tmpDirectory) { 
     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL]; 
    } 
} 
+0

是這樣意味着我需要手動處理它?那就是當下載發現設備中沒有足夠的空間時,iOS是否會清除'tmp'文件夾? –

+2

我想如果你不能刪除文件,然後iphone處理它。 **當需要更多空間時,會自動刪除緩存的文件**請參閱此[鏈接](https://support.apple.com/zh-cn/HT201656) – viratpuar

+0

從上一個[link](https://support.apple .com/zh-cn/HT201656),_如果要從iOS設備中刪除緩存文件 當您的iOS設備需要更多空間時,會自動刪除緩存文件。你不需要自己刪除它們。任何人都可以提供更多的細節嗎? –

相關問題