NSURLSession with invalid resume data

I am using [NSURLSessionConfiguration defaultSessionConfiguration] to configure url session.

I pause the task by calling cancelByProducingResumeData: to create the resume data and save it to disk. When I want to restart the task, I call downloadTaskWithResumeData: It works well until I restart the application.

I will kill the application after pausing the job. Then I started the application again and called downloadTaskWithResumeData , I found that the renewal data is invalid.

I parse the summary data in an NSDictionary and get an NSURLSessionResumeInfoLocalPath which

"/private/var/mobile/Containers/Data/Application/5DD071C3-9D5E-4D76-9F74-57B6C92445CB/tmp/CFNetworkDownload_IUI6kg.tmp" . I am trying to access this file but it does not exist.

My question is: how can I continue downloading using renewable data after restarting my application.

Thanks.

+5
source share
2 answers

Any time you restart the application, everything under tmp will be cleared, I experienced the same thing, although I tried to copy all * .tmp and paste it back whenever the application restarts, this will cause an error

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

+1
source

I ran into this problem. I found that the path to the sandbox changes after restarting the application in iOS8. But resumeData writes the old sandbox path, which allows the download job to not find resumeData. Therefore, I update the sandbox path recorded in the sandbox with the " NSURLSessionResumeInfoLocalPath , it works:

 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]; 
+1
source

Source: https://habr.com/ru/post/1212773/


All Articles