NSURLSession具有无效的简历数据 [英] NSURLSession with invalid resume data
问题描述
我使用 [NSURLSessionConfiguration defaultSessionConfiguration]
配置我的url会话。
我通过调用 cancelByProducingResumeData:
暂停一个任务来生成一个简历数据,并将其保存到磁盘上。当我要重新启动任务时,我调用 downloadTaskWithResumeData:
。它工作得很好,直到我重新启动应用程序。
我暂停一个任务后,我杀死了应用程序。然后我再次启动我的应用程序,并调用 downloadTaskWithResumeData
,我发现简历数据无效。
我解析将简历数据转换为NSDictionary并获取 NSURLSessionResumeInfoLocalPath
,这是
/ private / VAR /移动/容器/数据/应用/ 5DD071C3-9D5E-4D76-9F74-57B6C92445CB / TMP / CFNetworkDownload_IUI6kg.tmp
。我尝试访问此文件,但不存在。
我的问题是在重新启动应用程序后,如何使用简历数据继续下载任务。 >
谢谢。
我遇到这个问题。我发现在iOS8应用程序重新启动之后,沙箱路径会改变。但是resumeData记录了旧的沙箱路径,那就让下载任务找不到resumeData。所以我通过键' NSURLSessionResumeInfoLocalPath '更新记录在沙盒中的沙箱路径,它可以正常工作:
NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:resumeDataPath];
NSString * resumeDataFileName = [dic [@NSURLSessionResumeInfoLocalPath] lastPathComponent];
NSString * newTempPath = NSTemporaryDirectory();
NSString * newResumeDataPath = [newTempPath stringByAppendingPathComponent:resumeDataFileName];
[dic setValue:newResumeDataPath forKey:@NSURLSessionResumeInfoLocalPath];
[dic writeToFile:resumeDataPath atomically:YES];
I use [NSURLSessionConfiguration defaultSessionConfiguration]
to config my url session.
I pause a task by calling cancelByProducingResumeData:
to produce a resume data, and save it to the disk. When I want to restart the task, I call downloadTaskWithResumeData:
. It works well until I restart the app.
I kill the app after I pause a task. Then I start my app again, and call downloadTaskWithResumeData
, I found that the resume data was invalid.
I parse the resume data into NSDictionary and get the NSURLSessionResumeInfoLocalPath
, which is
"/private/var/mobile/Containers/Data/Application/5DD071C3-9D5E-4D76-9F74-57B6C92445CB/tmp/CFNetworkDownload_IUI6kg.tmp"
. I try to access this file, but it is not exist.
My question is how can I continue a download task using resume data after I restart my app.
Thanks.
I encountered this problem. I found that sandbox path will change after the application restart in iOS8. But the resumeData record the old sandbox path, that let the download task can’t find the resumeData. So I update the sandbox path recorded in sandbox by key ‘NSURLSessionResumeInfoLocalPath’, it does work:
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:resumeDataPath];
NSString *resumeDataFileName = [dic[@"NSURLSessionResumeInfoLocalPath"] lastPathComponent];
NSString *newTempPath = NSTemporaryDirectory();
NSString *newResumeDataPath = [newTempPath stringByAppendingPathComponent:resumeDataFileName];
[dic setValue:newResumeDataPath forKey:@"NSURLSessionResumeInfoLocalPath"];
[dic writeToFile:resumeDataPath atomically:YES];
这篇关于NSURLSession具有无效的简历数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!