开发者

Release a listener / observer in Objective-C?

I'm not sure when it's the right moment to RELEASE a listener object.

I have a object A that uses NSURLConnection's initWithRequest method to retrieve some URL. initWithRequest requires a delegate to listen after the events of dataReceived... So object A creates an object B and passes it as a delegate for initWithRequest method.

When the data is retrieved from the network a method of object B is开发者_JAVA技巧 called. After object B has completed its work who has the responsability to release object B?!?

TO SUMMARIZE:

object A creates object B and make it listener for some event. The event happens and object B makes its job. After object B has done its job who has the responsability to release it?!?

PLEASE NOTE There are many questions and answers on how to remove Observers in Objective-C. Anyway all of them I found they assumed you are using the KVO pattern.


Have you tried having object B release itself in the 'done receiving data' method? That would seem to be the end of its useful life. Or, you could maintain a reference to it in object A, and then release it in object A's dealloc method.


According to the NSURLConnection Reference:

The connection retains delegate. It releases delegate when the connection finishes loading, fails, or is canceled.

NSURLConnection is an exception in this regard - most objects do not retain their delegates.

So, in this case, object A should retain object B if A wants to keep using B, in which case it should release it when done; NSURLConnection will take care of its own use of B.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜