iOS NSURLSession,如何重试didCompleteWithError [英] iOS NSURLSession, how to retry in didCompleteWithError

查看:280
本文介绍了iOS NSURLSession,如何重试didCompleteWithError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器上尝试邮寄呼叫,直到成功为止,我想每30秒尝试一次.

所以我正在使用NSURLSession进行通话:

 NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:request];
task.taskDescription = [NSString stringWithFormat:@"Downloading file %@", [path lastPathComponent]];
[task resume];

然后,如果发生错误(例如,没有网络),则我在以下地方出现错误:

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

我的问题是我的应用程序将在后台运行的30秒内如何进行相同的通话?

我尝试使用dispatch,NSThread和performSelector,但是看起来它在后台不起作用:(

谢谢.

解决方案

一些观察结果:

  1. 如果您想在30秒后再试一次,即使该应用程序不再在前台运行,您可能也只是想要求OS额外一点时间来执行有限长度的任务.参见执行有限长度的任务.如果这样做,则标准计时器dispatch_after或其他任何方法都应该可以正常工作.

    请注意,您只有3分钟的时间来完成此后台任务.我知道您说过,您倾向于在30秒后再试一次,但是我个人怀疑如果用户没有连接,那么他们很有可能在接下来的几分钟内也没有连接./p>

  2. 顺便说一句,如果在应用程序仍在运行时重试,则建议不要在30秒内重试.我认为更好的方法是使用可达到性这样,当网络可用时,您会收到通知.在网络重新建立之前没有必要再尝试.

  3. 另一种方法是使用NSURLSessionConfiguration中的backgroundSessionConfigurationWithIdentifier.参见在后台下载内容.如果这样做,您有3分钟以上的时间来完成请求.重新建立连接后,它将自动开始.这种技术的缺点是:(a)后台会话的响应性不如前台会话; (b)仅限于iOS 7和更高版本; (c)您必须实现NSURLSession的基于委托的翻译,而不是完成块翻译,这需要更多的工作.

坦率地说,我通常倾向于采用第三种方法,但是每种方法都有其优点.

I want to try a post call on my server till it success, I would like to try it each 30 seconds.

So I am using NSURLSession for my call:

 NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:request];
task.taskDescription = [NSString stringWithFormat:@"Downloading file %@", [path lastPathComponent]];
[task resume];

Then if an error occur (no network for example) I have an error in:

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

My question is how to do the same call in 30 seconds will my app is in background?

I tried with dispatch, NSThread and performSelector but looks like it does not work in background :(

Thanks.

解决方案

A few observations:

  1. If you want to try again in 30 seconds, even if the app is no longer in the foreground, you will presumably just want to ask the OS for a little extra time to perform a finite length task. See Executing Finite-Length Tasks. If you do that, your standard timer, dispatch_after, or whatever, should work fine.

    Note you only have 3 minutes to complete this background task. I know you said that you would be inclined to just try again in 30 seconds, but I personally suspect that if a user doesn't have connectivity, there's a good chance that they might not have connectivity within the next few minutes, either.

  2. By the way, if retrying while the app is still running, I might advise against trying again in 30 seconds. Better, in my opinion, is to use Reachability so that you are notified when the network becomes available. There's no point in trying again until the network is re-established.

  3. Another approach is to use backgroundSessionConfigurationWithIdentifier of NSURLSessionConfiguration. See Downloading Content in the Background. If you do this, you have more than 3 minutes for the request to finish. And it will automatically start as soon as connectivity is re-established. The disadvantages of this technique are that (a) the background sessions aren't quite as responsive as foreground sessions; (b) it's limited to iOS 7 and later; and (c) you have to implement delegate-based rendition of NSURLSession rather than completion block rendition, which is a little more work.

Frankly, I'd generally be inclined to pursue the third approach, but each has its merits.

这篇关于iOS NSURLSession,如何重试didCompleteWithError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆