5

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.

Kampai
  • 22,848
  • 21
  • 95
  • 95
Mark
  • 51
  • 3
  • Thanks @Mark, good question - upvoted. Can you tell me how you write data to disk if user quits the App ? – NSPratik Jan 28 '16 at 08:43

2 Answers2

1

Anytime you relaunch your app, everything under tmp will be cleaned, I was experiencing the same thing, even though I tried to copy all the *.tmp, and paste back whenever app relaunches, it'll throw an error

My advice is that, you check if the *.tmp file is accessible, redownload from start if not

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
  • @LittleBobyTables, thanks a lot for editing my answers to be a lot nicer. Mark, I endup using [ASIHTTPRequest](https://github.com/pokeb/asi-http-request) to achieve what you & I both had in mind – johnny lang Feb 18 '15 at 17:53
  • Thank you for your advice. Can I rebuild the resumeData and replace the the *.tmp file path? – Mark Mar 04 '15 at 09:15
1

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];
Stoull
  • 1,098
  • 8
  • 13