重用NSURLConnection的实例 [英] Reusing an instance of NSURLConnection

查看:66
本文介绍了重用NSURLConnection的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iPhone上的NSURLConnection实例从服务器请求数据,该服务器照常由委托人进行管理.请求非常频繁(也许每2分钟说一次),并且具有通用且固定的URL.而不是看到每次下载后都会释放NSURLConnection的好实例,然后再创建一个新实例:

I'm using an instance of NSURLConnection on the iPhone to request data from a server, managed by a delegate as usual. The requests are quite frequent (maybe once every 2 minutes say) and have a common and fixed URL. Rather than seeing the good instance of NSURLConnection being released after each download and then a new one being created:

  1. 保留第一个连接并重新使用它是否有价值? (我希望如此,一个好的身份验证应该值一千.)

  1. Is there any worth in retaining the first connection and reusing it? (I'd hope so, one good authentication should be worth a thousand.)

如果是,该如何重用? docs中的突出方法是-start,但是在已使用过(且非null)的NSURLConnection实例上调用时,这似乎会使应用程序崩溃. [文档确实说-start如果没有,则使接收器开始加载数据."]

If so, how do I reuse it? The standout method in the docs is -start but this seems to crash the app when called on an already used (and non-nil) instance of NSURLConnection. [The docs do say -start "causes the receiver to begin loading data, if it has not already."]

如果对上述问题有所帮助,我(是!)建议:

In case it's of help with regard to the above questions, I am (was!) proposing:

if (connection_ == nil)
   {
    connection_ = [NSURLConnection connectionWithRequest:request
                                                delegate:self];
   }
  else
   {
    [connection_ start];
   }

推荐答案

文档似乎说URL连接保留了它的委托(非常规,但在这种情况下是必需的),然后在连接完成加载,失败或失败时将其释放.取消.

The docs seems to say that the URL connection retains it's delegate (unconventional, but necessary in this case) and then releases it when the connection finishes loading, fails or is cancelled.

问题在于,委托不是NSURLConnection上的可设置属性,因此释放它后就无法重置它.一旦运行一次,这几乎使URL连接无用,如果您想再次执行该操作,则需要释放并重新创建它.

The problem is that the delegate isn't a settable property on NSURLConnection and so you can't reset it after it's been released. This pretty much renders the URL connection useless after it has run once, requiring you to release and recreate it if you want to do it again.

这篇关于重用NSURLConnection的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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