我们编写了一个媒体应用程序,允许您使用 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];
最佳答案
在从 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/