iOS 后台传输 - com.apple.nsurlsessiond 文件夹充满 tmp 文件

标签 ios background-fetch

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

然后它使用 BACKGROUND TRANSFER 告诉 iOS 一个一个地下载视频并返回休眠状态,并在完成后唤醒应用。

它完成了所有这些工作,但我们注意到空间使用量在不断增长。

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

我们使用 Xcode > Organizer> Devices 下载应用程序文件夹,发现 BACKGROUND TRANSFER tmp 文件夹中的 tmp 文件很乏味。

这些不应该被清除吗

这通常是我使用的代码。 我认为主要是我将多个 DownloadTask(最多 30 个)附加到一个后台 session 。文件的大小从电影到 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

最佳答案

在从 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声明您应该“在从此委托(delegate)方法返回之前将文件移动到应用程序沙箱容器目录中的永久位置”(可能是Documents目录)。

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

关于iOS 后台传输 - com.apple.nsurlsessiond 文件夹充满 tmp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210472/

相关文章:

xcode - 我可以在没有开发人员凭据的情况下签署 IOS 应用程序吗?

ios - 如何访问 tableView 内部集合中的 ViewController 类变量和函数

ios - 将数据发送到服务器/在后台重试请求的选项

ios - UIApplication 应用状态返回错误值

ios - 使用 CALayer 显示 CGPaths 会影响 iOS 的性能吗?

iOS 7 文本工具包 : When has NSLayoutManager filled its last NSTextContainer?

ios - 使用 drawInRect 对齐文本 :withAttributes:

ios - 后台任务执行未按预期进行

ios - 在ios后台应用程序时将用户位置发送到服务器

ios - 后台获取超过30秒