Reusing an instance of NSURLConnection

蓝咒 提交于 2019-12-08 19:51:26

问题


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. Is there any worth in retaining the first connection and reusing it? (I'd hope so, one good authentication should be worth a thousand.)

  2. 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];
   }

回答1:


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.

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.



来源:https://stackoverflow.com/questions/5188420/reusing-an-instance-of-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!