Manual retain with ARC Manual retain with ARC ios ios

Manual retain with ARC

I have occasionally needed to manually retain and release things (sometimes just for debugging) and came up with the following macros:

#define AntiARCRetain(...) void *retainedThing = (__bridge_retained void *)__VA_ARGS__; retainedThing = retainedThing#define AntiARCRelease(...) void *retainedThing = (__bridge void *) __VA_ARGS__; id unretainedThing = (__bridge_transfer id)retainedThing; unretainedThing = nil

This works by using the __bridge_retained and __bridge_transfer to cast things to and from (void *) which causes things to be retained, or to create a strong reference without calling retain.

Have fun, but be careful!

Why not just assign your delegate object to a strong ivar for the duration of the asynchronous task?

Or have a local variable in executeAsyncWork

- (void)executeAsyncWork{    id localCopy = _delegate;    if (localCopy != nil) // since this method is async, the delegate might have gone    {        // do work on local copy    }}

Something like this:

- (void)startAsyncWork{    id<YourProtocol> delegate = _delegate;    dispatch_async(/* some queue */, ^{        // do work        [delegate doSomething];    }}

The block will retain the delegate as long as needed...