简体   繁体   English

在后台会话无效的情况下如何恢复数据下载

[英]How to resume data downloading, while having background session invalidated

Hi everyone it's my first expression on stack :). 大家好,这是我在堆栈上的第一个表达式:)。 I've searched lot of topics and didn't find answer. 我搜索了很多主题,但没有找到答案。 Ok, here is my problem: 好的,这是我的问题:

In my app I'm using NSURLSession - with background downloading. 在我的应用中,我正在使用NSURLSession-进行后台下载。 When I use cancelByProducingResumeData , everything goes fine, I saved resumeData in my sandbox directory, and resumed it easily with 当我使用cancelByProducingResumeData时 ,一切正常,我将resumeData保存在我的沙箱目录中,并使用

[session downloadTaskWithResumeData: resumeData];

What is important I didn't invalidate session object. 重要的是我没有使会话对象无效。 After app relaunch I'm creating new session and trying to resume downloading with same method downloadTaskWithResumeData: , obtaining message: 应用重新启动后,我正在创建新会话,并尝试使用相同的方法downloadTaskWithResumeData:继续下载,获取消息:

Invalid resume data for background download. 无效的简历数据可供后台下载。 Background downloads must use http or https and must download to an accessible file. 后台下载必须使用http或https,并且必须下载到可访问的文件。

Any ideas? 有任何想法吗? :) :)

I'm using background session and same ID. 我正在使用后台会话和相同的ID。 I already manage to solve problem. 我已经设法解决问题了。 Well I was struggling with it when running simulator, this is rather important info. 好吧,当我运行模拟器时,我一直在努力,这是非常重要的信息。 I save my resumeData in Library directory, and after reading it at runtime, it appears that NSURLSessionResumeInfoLocalPath key is referring to directory that doesn't exist. 我将我的resumeData保存在Library目录中,在运行时读取它之后,似乎NSURLSessionResumeInfoLocalPath密钥引用的目录不存在。 Here is my solution: 这是我的解决方案:

NSMutableDictionary *resumeDictionary = [NSPropertyListSerialization propertyListWithData:invalidResumeData options: NSPropertyListMutableContainersAndLeaves format:NULL error:&error];
if (!resumeDictionary || error) return nil;


NSString *localTmpFilePath = [resumeDictionary objectForKey:@"NSURLSessionResumeInfoLocalPath"];
if ([localTmpFilePath length] < 1) return nil;


NSString *localName = [localTmpFilePath lastPathComponent];
NSString *tempPath = NSTemporaryDirectory();

NSString *tempFile = [tempPath stringByAppendingString: [NSString stringWithFormat: @"%@", localName]];
[resumeDictionary setValue: tempFile forKey: @"NSURLSessionResumeInfoLocalPath"];

NSString *libDirectory = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
libDirectory = [libDirectory stringByAppendingString: [NSString stringWithFormat: @"/%@/%@.tmp", kPathToResumeData, self.guid]];

NSData *newResumeData = [NSPropertyListSerialization dataWithPropertyList: resumeDictionary
                                                                   format: NSPropertyListBinaryFormat_v1_0
                                                                  options: 0
                                                                    error: &error];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM