Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One general note on the strong vs. weak reference: strong references denote <em>ownership</em>, while weak references denote <em>association</em>. When neither object owns the other, there's usually other objects that own them, or they both have strong references coming from local variables.</p> <blockquote> <p>Wouldn't [the <code>Person</code> class] lead to a retain cycle?</p> </blockquote> <p>Yes, it would, because a <code>Person</code> owns his brother, but he shouldn't: this should be a weak property. It should be OK, because there should be another object (a list of all persons, a dictionary organizing persons by name, or something like it) that owns all of the <code>Person</code> objects. As long as a <code>Person</code> object is in that collection of persons, it's not going to be released.</p> <blockquote> <p>Wouldn't [<code>Appointment</code> and <code>Staff</code>] lead to a retain cycle since all the references are strong?</p> </blockquote> <p>Correct, that is what is going to happen. <code>NSArray</code> retains objects that go into it, closing the loop on the retain cycle. Note that you cannot make <code>NSArray</code> weak to break that cycle: it needs to be <code>Staff</code> that gets weak, not <code>appointments</code>.</p> <blockquote> <p>Finally, consider this scenario: Let's say I change <code>Appointment</code>'s <code>staff</code> property to <code>weak</code>. This may solve the issue for my second (above scenario), but what if I am creating a new appointment and I want to attach a staff member, then pass the object somewhere else for processing?</p> </blockquote> <p>There's no problem with that: stack variable <code>staff</code> has a strong reference on it, so it wouldn't get released. </p> <blockquote> <p>Because my <code>staff</code> property on Appointment is now weak, is there the potential for it to be set to nil when garbage collection runs (as there are no strong references to the staff object)?</p> </blockquote> <p>ARC is not a garbage collection system: retains and releases happen at specific, deterministic time points. The <code>staff</code> is not going to be released, because <code>Staff *staff = [Staff new];</code> is strong by default.</p> <p><strong>EDIT :</strong> The edited example will indeed release the <code>Staff</code> object. However, such example is unusual, because it is unlikely that your <code>createAppointment</code> would make a new instance of <code>Staff</code>. Rather, it would grab an existing instance from some registry containing all members of staff, like this:</p> <pre><code>+ (Appointment *)createAppointment { Appointment *appt = [Appointment new]; // Registry gives you a reference to an existing Staff. // Since the object remains registered, that's not the last strong reference. Staff *staff = [staffRegistry reserveMember]; appt.staff = staff; return appt; } </code></pre> <p><code>staffRegistry</code> is a class that manages (and owns) all <code>Staff</code> objects, keeping them in an array, a dictionary, or some other collection. All other references to <code>Staff</code> objects (apart from temporary references by stack variables) should be weak. This way removing a member from the registry will also release him from all the appointment objects in which he may participate.</p> <blockquote> <p>My co-workers seem to have handled it by using two properties, one strong and the other weak</p> </blockquote> <p>If you are thinking it's a hack, you are 100% right. The strong vs. weak issue gets straightforward once you make a decision on who knows what; you don't need to come up with code that promises some serious maintenance nightmare to address it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload