我们已经编写了一个媒体应用程序,它允许您使用后台FETCH获取最新视频列表为json列表
然后,它使用后台传输告诉iOS一个一个下载视频,然后回去睡觉,当它完成时唤醒应用程序。
这一切都是这样的,但我们注意到,空间的使用在不断增长。
我们添加了代码来清除所有下载的视频,但空间使用保持在设置中。
我们使用Xcode > Organizer>设备下载了应用程序文件夹,发现后台传输tmp文件夹对tmp文件很枯燥。
这些不应该被清理掉吗
这通常是我使用的代码。我认为主要是将多个DownloadTask(最多可达30)附加到一个后台会话。文件大小不同,从电影到pdfs。
NSURLSession * backgroundSession_ = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
backgroundSession_ = [NSURLSession sessionWithConfiguration:urlSessionConfigurationBACKGROUND_
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
NSOperationQueue *mainQueue_ = [NSOperationQueue mainQueue];
NSURLSessionDownloadTask * downloadTask_ = [backgroundSession_ downloadTaskWithURL:url_];
downloadStarted_ = TRUE;
[downloadTask_ resume];
发布于 2015-07-30 14:55:05
我也有同样的问题,但在稍微不同的情况下:在较旧的设备(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文件仍然未被跟踪,因此需要通过这样的方法来删除这些文件。
顺便说一句,这似乎是同一个问题--没有结论。
https://stackoverflow.com/questions/31210472
复制相似问题