iOS后台传输 - com.apple.nsurlsessiond文件夹中包含tmp文件

时间:2015-07-03 15:47:54

标签: ios background-fetch

我们编写了一个媒体应用程序,允许您使用BACKGROUND FETCH获取最新视频列表作为json列表

然后它使用BACKGROUND TRANSFER告诉iOS逐个下载视频并重新进入睡眠状态并在应用完成后唤醒它。

它完成了所有这些,但我们注意到空间使用正在增长和增长。

我们添加了代码来清除所有下载的视频,但空间使用情况在设置中保持不变。

我们使用Xcode>下载了应用文件夹。组织者>设备并发现BACKGROUND TRANSFER tmp文件夹对tmp文件很无聊。

这些不应该被清除

这通常是我使用的代码。 我认为主要是我将多个DownloadTask(最多可以30个)附加到一个后台会话。文件大小从电影到pdf不等。

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];

enter image description here

2 个答案:

答案 0 :(得分:1)

在从didFinishDownloadingToURL返回之前尝试这样的事情:

// App's Documents directory path
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];

// Creating the path for the downloaded file
docsPath = [docsPath stringByAppendingPathComponent:downloadTask.response.suggestedFilename];

// Moving the file from temp location to App's Documents directory
[[NSFileManager defaultManager] moveItemAtPath:location.path toPath:docsPath error:NULL];

documentation表示您应该&#34;将文件移动到应用沙盒容器目录中的永久位置,然后再从此委托方法返回&#34; (也许< strong>文件目录)。

didFinishDownloadingToURL返回后(或下载失败时)清除的临时文件 - 由操作系统自行决定(通常在内存压力下)。

答案 1 :(得分:0)

我有同样的问题,但情况有点不同: 在旧设备(iPhone 4S或更早版本)上,应用程序通常在操作系统的后台获取期间被杀死。可能是为了释放记忆。在这种情况下,tmp文件被保留(并且未跟踪)。下次应用程序有机会获取时,会创建新文件...此循环会一直持续到用户识别该应用程序使用4gb存储空间 - 并将其删除。

我还没有找到完美的解决方案 - 即使我将后台配置的 -NSURLSessionConfiguration URLCache 设置为自定义的(文档说它默认为nil),并且路径位于同一目录下(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文件仍然没有跟踪,因此需要通过这样的方法删除它们。

顺便说一下,this似乎是同一个问题 - 没有结论。