Can any one explain Retain cycle with example code(Objective C) ? and How can we remove retain Cycle ?(with code or diagram). I know about it theoretically but i never come across such kind of program ? I am very curious to see, how retain cycle solved (with code or diagram) ?
Share
Delegation is one example where you have to avoid a retain cycle by using the
assignattribute on a delegate property. For example, you have a parent object which creates a child:So the parent has a retained reference to the child (because the property setter retains it).
Now the parent sets itself as a delegate on the child:
Now, if the child retains its
delegateproperty there is a retain cycle. Both contain references to the other and cannot be deallocated.To avoid this the child declares the delegate property with the
assignattribute:This is safe because a delegate will almost always outlive the delegator. If not, the parent should set the child’s delegate to nil before it goes away.