简体   繁体   English

何时释放NSUrlConnection对象

[英]When to release NSUrlConnection object

What is the correct point at which to release a NSUrlConnection object? 释放NSUrlConnection对象的正确点是什么?

In my program, I alloc a NSUrlConnection, and then initWithRequest to kick off asynchronously. 在我的程序中,我分配一个NSUrlConnection,然后initWithRequest异步启动。

I am now responsible for releasing the object - when do I/can I release? 我现在负责释放对象 - 我什么时候可以释放? Immediately if I'm not using it again? 如果我不再使用它,请立即?

When you are done with the NSURLConnection . 完成NSURLConnection You'll know you're done with it when your NSURLConnection delegate receives - (void)connectionDidFinishLoading:(NSURLConnection *)connection or - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error . NSURLConnection委托收到- (void)connectionDidFinishLoading:(NSURLConnection *)connection- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error时,你就会知道你完成了它。 Just release it at the end of those two methods. 只需在这两种方法结束时释放它。

Your usage of the NSUrlConnection can be released immediately. 您可以立即释放您对NSUrlConnection的使用。

The framework will release when it has finished with it. 该框架将在完成后发布。

您不需要释放它,直到框架向您发出通知,例如, connection:didFailWithError: .

This is a bit late (your question was posted in 2009, over a year ago) but I was having the some trouble with my NSUrlConnection code. 这有点晚了(你的问题发布于2009年,一年多前)但我的NSUrlConnection代码遇到了一些麻烦。 NSUrlConnection kept crashing and I thought the crash was related to my release of NSUrlConnection [theConnection release]; NSUrlConnection一直崩溃,我认为崩溃与我发布的NSUrlConnection [theConnection release]; . I was writing my code as in Apple's example here , but I ended up releasing the connection after it was created in "Listing 1" rather than releasing it where Apple does in both didFailWithError and connectionDidFinishLoading and it seems to work fine. 我在写我的代码,如苹果公司的例子在这里 ,但是我最终释放连接,这是“清单1”,而不是将其释放,其中苹果确实在无论是在创建后didFailWithErrorconnectionDidFinishLoading ,似乎很好地工作。 I'm not completely sure, but maybe both of those delegates were being called and a double release was what was causing the crash. 我不完全确定,但也许这两个代表都被调用了,双重释放是导致崩溃的原因。 I wasn't sure I could release my NSUrlConnection object right after it was created until I read this question but that seemed to do the trick. 我不确定我可以在创建NSUrlConnection对象之后立即释放它,直到我读到这个问题,但这似乎可以解决问题。

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

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