Before ARC I had the following code that retains the delegate while an async operation is in progress:
- (void)startAsyncWork
{
[_delegate retain];
// calls executeAsyncWork asynchronously
}
- (void)executeAsyncWork
{
// when finished, calls stopAsyncWork
}
- (void)stopAsyncWork
{
[_delegate release];
}
What is the equivalent to this pattern with ARC?
Something like this:
The block will retain the delegate as long as needed...
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
I have occasionally needed to manually retain and release things (sometimes just for debugging) and came up with the following macros:
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!