我有同样的问题,但在一些不同的情况下: 在较旧的设备(iPhone 4S或更旧版本)上,应用程序通常在操作系统获取后台进程期间被终止。可能会释放记忆。在这种情况下,tmp文件被保留(和未被跟踪)。下一次该应用程序有机会获取,创建新文件......并且这个循环持续进行,直到用户认识到该应用程序使用4GB的存储空间 - 并将其删除。
我还没有找到完美的解决方案还没有 - 即使我设置了后台配置的-NSURLSessionConfiguration Urlcache文件到一个自定义(文件说,这是默认无)与路径相同的目录(defaultCacheDir/com.apple .nsurlsessiond/...)被使用 - 但是当我确定没有下载正在进行的时候,做了一个清理方法并使用它。
+ (BOOL)clearCache:(NSError * __autoreleasing *)error
{
__block BOOL successOnLegacyPath = NO;
__block NSError *errorOnLegacyPath = nil;
NSString *cacheDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *allSubPaths = [[NSFileManager defaultManager] subpathsAtPath:cacheDirPath];
if (!allSubPaths) {
NSLog(@"No subpaths of cache:\n%@", cacheDirPath);
} else {
[allSubPaths enumerateObjectsUsingBlock:^(NSString *subpath, NSUInteger idx, BOOL *stop) {
static NSString * const kNSURLSessionPathComponent = @"nsurlsession"; // this is a non-documented way, Uncle Apple can change the path at any time
if ([subpath containsString:kNSURLSessionPathComponent]) {
successOnLegacyPath = [[NSFileManager defaultManager] removeItemAtPath:[cacheDirPath stringByAppendingPathComponent:subpath]
error:&errorOnLegacyPath];
if (!successOnLegacyPath) {
NSLog(@"Error while deleting cache subpath:\n%@\nError:\n%@", subpath, errorOnLegacyPath);
}
// First we find is the root > bail out
*stop = YES;
}
}];
}
if (!successOnLegacyPath && !errorOnLegacyPath) {
// Couldn't find the nsurlsession's cache directory
if (error) *error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFileNoSuchFileError
userInfo:nil];
// OR
successOnLegacyPath = YES;
}
return successOnLegacyPath;
}
这不是一个解决方案,这是建议,如果没有下载正在进行中使用。如果正在运行下载并尝试删除tmp文件,则未测试发生了什么。
即使我找到解决方案,以前创建的tmp文件仍然未被跟踪,所以需要使用这种方法删除这些文件。
Btw,this似乎是同一个问题 - 没有结论。