When assign(weak) and retain(strong) are expected to be used in Obj-C/Cocoa programming

Well, for last 2 years, many things have been changed in Mac and iOS programming world. We could see that Apple people tried somewhat different approach whenever they introduced new iOS SDKs, etc. We can guess it by looking at how they prepare initial template source codes for newly created project, for example, omitting declaration of instance variable, that is member variable, saying that @property declaration makes it kind of redundant, etc.
While things are changed like that there are some things for us to memorize or understand. As mentioned in my previous post, “retain” for the top most variable pointed by File Owner in xib becomes “assign” or “weak”. It’s kind of difficult to memorize why it was assign for Mac and retain for iOS.
Introduction of blocks also introduced that. _block variable is to be “assigned” for traditional retain-release mechanism and “strong” ( thus “retain” semantically ) in ARC.

Why is it that?
I kind of saw some similarity between those, i.e the IBOutlet variables and _block variables, or any others I didn’t mention already.

  • assign/weak : When the framework or their mechanism wants to do some easier housework like cutting links etc under retain-release mechanism
  • retain/strong : When their infrastructure for managing memory is evolved sufficiently and handles (more) completely.

For the 1st case, which is assign/weak, some entity ( probably some manager entity in the runtime ) wants to clean up memory pointed by those reference variables, there will be less headache if they can just gives up the reference or hand-over some entity to handle the cleaning up process. So, whether it is those top-most IBOutlets or _block, this looks to be the way the hidden internal entity of runtime does.

For the 2nd case, which is retain/strong, the ARC mechanism or autorelease pool manager or a interested entity in the Obj-C runtime or even the OS itself look to be in charge of managing those more. So, because they have the ownership of those variables, they are responsible for releasing memory space pointed by those variables.

Probably the mind-set of the S/W engineers at Apple who designed the behavior is, “Oh, with the new mechanism, one of our entity manages them and they are empowered sufficiently.” for the new mechanism ( retain/strong ) and the “Oh.. we believe something else will handle it. We are not responsible with those variables.” for the old mechanism. (retain/strong)

If you have some hard time in memorizing which behavior (weak or strong?) you should be considering while you write code, thinking the way I described here can be helpful. I think it works mostly. :)

About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: