为什么要在这个语句中释放NSURLConnection实例呢?

发布于 2024-09-02 01:24:39 字数 758 浏览 5 评论 0原文

我在一本书上读到过这个。

-(IBAction) updateTweets
{
   tweetsView.text = @"";
   [tweetsData release]; 
   tweetsData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.xml" ]; 
   NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url]; 
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   [connection release];
   [request release]; 
   [activityIndicator startAnimating];
}

在这个语句中,当时释放“连接”实例是否正确?释放它之后,这意味着这个 NSURLConnection 实例将被销毁,因为它的引用计数为 0 ,我们如何在释放这个实例后使这个连接操作工作?谢谢。

我想我们应该在以下回调方法之一中释放它,对吧?

  • connectionDidFinishLoading:

  • connection:didFailWithError:

I read this in a book.

-(IBAction) updateTweets
{
   tweetsView.text = @"";
   [tweetsData release]; 
   tweetsData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.xml" ]; 
   NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url]; 
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   [connection release];
   [request release]; 
   [activityIndicator startAnimating];
}

In this statement,is that correct to release the "connection" instance at that time? After releasing it which means this NSURLConnection instance will be destroyed since it's reference count is 0 ,how are we going to make this connection operation work after release this instance? THANKS.

I thought we should release it in one of the following callback methods,right?

  • connectionDidFinishLoading:

  • connection:didFailWithError:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2024-09-09 01:24:39

实际上,在那里释放它是可以的,一旦通过 initWithRequest 发出连接,唯一重要的是委托是否存在,否则我相信响应将默默地丢失。

据我所知,不释放它的唯一原因是,如果您想在委托函数之一的某个时刻调用 [连接取消],在这种情况下,最好按照您的建议进行操作并在connectionDidFinishLoading 和 didFailWithError 两者,因为只会调用其中之一(对吗?)。

编辑:为了更彻底的答案, NSURLConnection initWithRequest 是一个异步请求。因此它实际上生成了自己的线程(但在调用 initWithRequest 的线程上调用委托函数)。所以基本上,在调用 initWithRequest 的线程上,您实际上已经完成了连接对象,并且可以释放它。它一直在其他线程上做一些你不需要关心的事情。

另外我应该注意,如果您确实在那里释放它,请确保不要在完成/失败方法中释放它,因为它不是有效的对象。

It's actually fine to release it there, once the connection is sent out via initWithRequest, the only thing that matters is that the delegate exists or I believe the response will just be silently lost.

From what I can tell, the only reason to not release it there is if you want to call [connection cancel] at some point in one of the delegate functions, in which case it would be best to do what you suggest and release it in BOTH connectionDidFinishLoading and didFailWithError since only one of them will be called (right?).

Edit: For a more thorough answer, NSURLConnection initWithRequest is an asynchronous request. So it actually spawns it's own thread (but calls the delegate functions on the thread that called initWithRequest). So basically, on the thread that calls initWithRequest you are actually done with the connection object and you can release it. All the while it's doing stuff on some other thread that you don't need to be concerned with.

Also I should note that if you do release it there, make sure you DON'T release it in the finish/fail methods, because it won't be a valid object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文