NSURLConnection是否保留其委托? [英] Does a NSURLConnection retain its delegate?
问题描述
我的问题摘要:NSURLConnection是否保留其委托?
Summary of my question: Does NSURLConnection retain its delegate?
详细的问题和场景:
我有一个自定义类,名为JsonDownloader,它接收一个URL和返回URL返回的JSON的NSDictionary。
I have a custom class, called JsonDownloader that is takes in a URL and returns an NSDictionary of the JSON that the URL returns.
在iPhone应用程序上,我执行类似这样的操作。 (init方法启动整个过程)
On an iPhone app, I do something like this. (the init method kicks off the whole process)
- (void)viewDidLoad {
JsonDownloder *temp = [[[JsonDownloader alloc] initWithURL:urlString returnDataTo:self]];
[temp release];
[super viewDidLoad];
}
当JsonDownloader完成下载和解析时,它会对returnDataTo执行回调:object,在这种情况下,是调用对象。
When the JsonDownloader is done downloading and parsing, it performs a callback to the returnDataTo: object, in this case, the calling object.
这很好用。即使我在我的Web服务器响应中引入了30秒的延迟,JsonDownloader仍然存在,并且正确回调。
This works just fine. Even if I introduce a 30 second delay in my web servers response, the JsonDownloader still exists and does it's callback correctly.
所以我的问题是:什么是保持JsonDownloader的方式在事件周期结束后?我明确地发布了它。
So my questions is this: What is keeping JsonDownloader way past the end of the event cycle? I am explicitly releasing it.
我的预感是NSURLConnection必须对其委托进行保留,但我没有在文档中看到任何内容。有人有想法吗?
My hunch is that NSURLConnection must do a retain on its delegate, but I didn't see anything in the documentation. Anyone have an ideas?
推荐答案
没有很多setter不会复制或保留传递给它的变量,以免当保留计数达到零时,将所述变量的内存重新分配给其他内容。
There aren't many setters that don't either copy or retain a variable being passed to it, lest the memory of said variable be re-allocated to something else when its retain count reaches zero.
然而,答案是肯定的,确实如此。一小段测试代码显示代表的保留计数上升:
However, the answer is YES, it does. A little bit of test code shows the delegate's retain count go up:
NSLog(@"Retain count before: %d", [self retainCount]);
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLConnection* conn = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"Retain count after: %d", [self retainCount]);
在日志中产生:
Running…
2009-07-09 02:13:40.516 delegateRetain[45123:a0f] Retain count before: 1
2009-07-09 02:13:40.525 delegateRetain[45123:a0f] Retain count after: 2
Debugger stopped.
所以你可以清楚地看到在 connectionWithRequest:delegate:
self的保留计数确实增加了+1。如果你觉得自己很勇敢并且想要使用EXC_BAD_ACCESS诸神,请加入
So you can see pretty clearly that in connectionWithRequest:delegate:
"self" is indeed having its retain count increased +1. If you're feeling brave and want to mess with the EXC_BAD_ACCESS gods, add in
[conn dealloc];
NSLog(@"Retain count after dealloc: %d", [self retainCount]);
将再次打印出1,显示一个dealloc减少后的值。但是,你会得到一个很好的程序接收信号:EXC_BAD_ACCESS。
因为 NSAutoreleasePool
将尝试释放连接,它将消失;)
which will print out "1" again, showing a post dealloc decrement. However, you'll get a nice Program received signal: "EXC_BAD_ACCESS".
because the NSAutoreleasePool
will try to release the connection and it will be gone ;)
这篇关于NSURLConnection是否保留其委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!