2015-07-03 76 views
13

我们已经写了一个媒体应用程序,允许您使用背景FETCHiOS的后台传输 - com.apple.nsurlsessiond文件夹的完全的tmp文件,

获得的作为JSON列表最新的视频列表,然后它使用后台传输到让iOS逐一下载视频,然后回去睡觉,并在完成后唤醒应用程序。

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

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

我们使用Xcode> Organizer> Devices下载了应用程序文件夹,发现BACKGROUND TRANSFER tmp文件夹是tmp文件的沉闷。

不应该被清除掉

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

​​

enter image description here

回答

0

尝试是这样的,从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状态,你应该“的文件移动到永久位置在应用程序的沙箱容器目录从这个委托方法返回之前“(也许Documents目录)。

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

0

我有同样的问题,但在一些不同的情况下: 在较旧的设备(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似乎是同一个问题 - 没有结论。