Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual retain with ARC

Tags:

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?

like image 712
Alexander Avatar asked Oct 17 '11 10:10

Alexander


People also ask

What does retain mean in Objective-C?

The system that Objective-C uses is called retain/release. The basic premise behind the system is that if you want to hold on to a reference to another object, you need to issue a retain on that object. When you no longer have a use for it, you release it. Similar to Java, each object has a retain count.

What is retain in swift?

You send an object a retain message when you want to prevent it from being deallocated until you have finished using it. An object is deallocated automatically when its reference count reaches 0 . retain messages increment the reference count, and release messages decrement it.

What is OBJC arc?

Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.


2 Answers

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!

like image 166
MagicSeth Avatar answered Oct 28 '22 22:10

MagicSeth


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     } } 
like image 37
JeremyP Avatar answered Oct 28 '22 22:10

JeremyP