Is it ever Ok to have a 'strong' reference for a delegate?

automatic-ref-counting, delegates, memory-management, objective-c, objective-c-blocks

Solution

Q1 - Yes. As you point out yourself having delegate properties being weak is a recommendation to help avoid retain cycles. So there is nothing wrong per se with having a strong delegate, but if the clients of your class expect it to be weak you may cause them surprises. The better approach is to keep the delegate weak and for the server side (the class with the delegate property) to keep a strong reference internally for those periods it needs one. As @Scott points out Apple documents doing this for `NSURLConnection`. Of course that approach doesn't solve your issue - where you want the server to retain the delegate for you...

Q2 - Looked at from the client side the problem is how to keep a delegate alive as long as a server with a weak reference to it requires it. There is a standard solution to this problem called associated objects. In brief the Objective-C runtime essentially allows a key-collection of objects to be associated with another object, along with an association policy which states how long that association should last. To use this mechanism you just need to pick your own unique key, which is of type `void *` - i.e. an address. The following code outline shows how to use this using `NSOpenPanel` as an example:

#import <objc/runtime.h> // import associated object functions

static char myUniqueKey; // the address of this variable is going to be unique

NSOpenPanel *panel = [NSOpenPanel openPanel];

MyOpenPanelDelegate *myDelegate = [MyOpenPanelDelegate new];
// associate the delegate with the panel so it lives just as long as the panel itself
objc_setAssociatedObject(panel, &myUniqueKey, myDelegate, OBJC_ASSOCIATION_RETAIN);
// assign as the panel delegate
[panel setDelegate:myDelegate];

The association policy `OBJC_ASSOCIATION_RETAIN` will retain the passed in object (`myDelegate`) for as long as the object it is associated with (`panel`) and then release it.

Adopting this solution avoids making the delegate property itself strong and allows the client to control whether the delegate is retained. If you are also implementing the server you can of course provide a method to do this, maybe `associatedDelegate:`?, to avoid the client needing to define the key and call `objc_setAssociatedObject` itself. (Or you can add it to an existing class using a category.)

HTH.

Problem

I have a class that retrieves JSON from a URL and returns the data via the protocol/delegate pattern. MRDelegateClass.h ``` #import <Foundation/Foundation.h> @protocol MRDelegateClassProtocol @optional - (void)dataRetrieved:(NSDictionary *)json; - (void)dataFailed:(NSError *)error; @end @interface MRDelegateClass : NSObject @property (strong) id <MRDelegateClassProtocol> delegate; - (void)getJSONData; @end ``` Note that I'm using `strong` for my delegate property. More about that later... I am trying to write a 'wrapper' class that implements getJSONData in a block-based format. MRBlockWrapperClassForDelegate.h ``` #import <Foundation/Foundation.h> typedef void(^SuccessBlock)(NSDictionary *json); typedef void(^ErrorBlock)(NSError *error); @interface MRBlockWrapperClassForDelegate : NSObject + (void)getJSONWithSuccess:(SuccessBlock)success orError:(ErrorBlock)error; @end ``` MRBlockWrapperClassForDelegate.m ``` #import "MRBlockWrapperClassForDelegate.h" #import "MRDelegateClass.h" @interface DelegateBlock:NSObject <MRDelegateClassProtocol> @property (nonatomic, copy) SuccessBlock successBlock; @property (nonatomic, copy) ErrorBlock errorBlock; @end @implementation DelegateBlock - (id)initWithSuccessBlock:(SuccessBlock)aSuccessBlock andErrorBlock:(ErrorBlock)aErrorBlock { self = [super init]; if (self) { _successBlock = aSuccessBlock; _errorBlock = aErrorBlock; } return self; } #pragma mark - <MRDelegateClass> protocols - (void)dataRetrieved:(NSDictionary *)json { self.successBlock(json); } - (void)dataFailed:(NSError *)error { self.errorBlock(error); } @end // main class @interface MRBlockWrapperClassForDelegate() @end @implementation MRBlockWrapperClassForDelegate + (void)getJSONWithSuccess:(SuccessBlock)success orError:(ErrorBlock)error { MRDelegateClass *delegateClassInstance = [MRDelegateClass new]; DelegateBlock *delegateBlock = [[DelegateBlock alloc] initWithSuccessBlock:success andErrorBlock:error]; delegateClassInstance.delegate = delegateBlock; // set the delegate as the new delegate block [delegateClassInstance getJSONData]; } @end ``` I've come to the objective-c world relatively recently (only lived in ARC times, and still coming to terms with blocks) and admittedly my understanding of memory management is on the slimmer side of things. This code seems to work fine, but only if I have my delegate as `strong`. I understand that my delegate should be `weak` to avoid potential retain-cycles. Looking in instruments, I find that allocations do not continue to grow with continued calls. However, I believe 'best practice' is to have `weak` delegates. Questions Q1) is it ever 'ok' to have `strong` delegates Q2) how could I implement the block-based wrapper leaving the delegate of the underlying class as `weak` delegate (ie. prevent the *delegateBlock from being deallocated before it receives the protocol methods)?

Original source