Strong vs. Weak


#1

I come from a Ruby and Rails background. I’m looking for affirmation of what I think is correct regarding strong versus weak.

In Rails, models have relationships. A model can have_one or can have_many models. The “owned” model, in turn, can belong_to another model. For example:

[code]class Product < ActiveRecord::Base
has_many: bins
end

class Bin < ActiveRecord::Base
belongs_to: product
end[/code]

If I’m understanding what I’m reading, Objective-C does the same thing. Strong roughly equivalates to Rails’ has_many or has_one, and weak roughly equivalates to Rails’ belongs_to. Am I correct?

Thank you in advance!


#2

Howdy.

The idea of strong vs weak is only relevant in the contexts of memory management and garbage collection.

Ruby has the WeakRef class that serves a similar purpose to a weak pointer in Objective-C.

Often, we use weak pointers in Objective-C to make a reference from a “child” object to a “parent,” so that we do not create a retain cycle in memory managed code (even with ARC).

If you’re using Objective-C on the Mac, with garbage collection, cycles are not as much of a problem, since the garbage collector is smart enough to deal with them. This is similar to the way that Ruby operates.

The relationships you mention from Rails are only for establishing connections between your model objects, and do not directly deal with memory management.

Hope this helps clear things up.


#3

Brian,

I think you missed the point of my post, but that’s okay. I understand what you’re saying in terms of Ruby, Rails, and memory management. Thank you for taking the time to respond. At this point, I’m confident I understand strong vs weak and its place in Objective-C.