0

Let's say I have some kind of static class, I mean .h class for my static library with all static functions. Some of function do async request to web service and using NSMutableURLRequest with initWithRequest:request. Since I want to monitor data coming from the server I create a delegate which implements all the NSURLConnectionDelegate connection protocol i.e. accept, append, finish and pass it to request.

The question is since I have all static methods in my .h class and I don't have a NSURLConnectionDelegate delegate as member (since class is static), where do I release my connection delegate? and the main question is it a common practice to do a self release on NSURLConnectionDelegate in connectionDidFinishLoading delegate method?

thanks

topsky
  • 199
  • 3
  • 12

2 Answers2

2

You can release the delegate right after assigning it to the NSURLConnection. From the documentation:

During a download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.

This is an exception to the general rule that objects only hold weak references to their delegates.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
0

It's not a normal practice to release the delegate, since it's not normal for objects to retain delegates. Delegates usually referenced with weak (__unsafe_unretained) attribute, so the reference is safely broken when object is released.

Kyr Dunenkoff
  • 8,090
  • 3
  • 23
  • 21
  • I send async request within static "+" method and I create there NSURLConnectionDelegate to pass to NSMutableURLRequest, so when connection finished loading data I don't have any possibility to release this delegate from outside of itself. So the only possibility ro release delegate is inside itself. So how do I deal with that? – topsky Aug 05 '12 at 09:15
  • So you using class factory method, so what. Create persistent delegate outside of that method and pass a delegate reference in your method's parameters like that: `[MyClass sendRequest:myRequest withDelegate:myDelegate]`. Your delegate should conform to `NSURLConnectionDelegate` protocol. – Kyr Dunenkoff Aug 05 '12 at 09:45
  • Thanks, that how I did it, but since I create a static library with .h external API I don't want to expose delegate internals to the client, only static functions that client call through .h API. Any suggestions? – topsky Aug 05 '12 at 10:20
  • Then create new class for your delegate in your library (`MyURLConnectionDelegate`), and make your request method accept only delegates of that class. In your case, delegate should be external, internal delegates would be immediately released after your method completes or you'll get zombies after. – Kyr Dunenkoff Aug 05 '12 at 10:33
  • Ok, thanks, but how I prevent from client not to override NSURLConnectionDelegate methods? if the client set delegate from the outside? – topsky Aug 05 '12 at 10:46
  • As I said earlier - make your request method accept only delegates of your delegate class. You know what that means, right? `if (![delegate isMemberOfClass:[MyURLConnectionDelegate class]]) return;` – Kyr Dunenkoff Aug 05 '12 at 10:49
  • NSURLConnection is an exception to the rule that delegates aren't retained. NSURLConnection maintains a strong reference to its delegate. – JeremyP Aug 05 '12 at 11:05
  • @JeremyP `NSURLConnection` object retains the delegate, not some other object, that's the issue here. – Kyr Dunenkoff Aug 05 '12 at 11:08
  • @KyrDunenkoff But it's the delegate that we are talking about. – JeremyP Aug 05 '12 at 11:33