Why use weak pointer for delegation?
I can’t understand why it is correct to define a delegate with weak pointer :
@property (nonatomic,weak) id delegate;
I can’t realize why isn’t necessary to retain a reference to the delegate… i don’t want the object that i use as the delegate to be deallocated… thus, i would prefer using a strong reference not a weak!
In many cases the delegate is the same object where the instance of my class will be created, in this case creating a weak reference would be a great solution to avoid retain cycle… but what if I choose a totally different object as the delegate ?
I searched for other questions on stack overflow but I can’t find something that can help me to fully understand this situation.
4 Solutions Collect From Internet About “Why use weak pointer for delegation?”
The reason that objects weakly retain their delegates is to avoid retain cycles. Imagine the following scenario: object
b and retains it, then sets itself as
a is released by its owner, leaving a retain cycle containing
b. This is actually a very common scenario. Consider a view controller that owns a view and acts as that view’s delegate. In this case, the view should not retain the controller—as a mater of proper MVC architecture and to prevent retain cycles.
Though retain cycles are a valid concern, the reasoning for a weak reference is more related to apple’s perspective on how to use the delegation pattern with uikit and other elements out of the box which is explained here:
“The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.”
If the delegate deals with managing the delegated tasks of several objects then those objects need not retain the delegate and should not bear the responsibility of dealllocating the delegate as it might be used by other objects. The weak reference enforces the concept that the management of the delegate is not the delegators responsibility.
An example in objective c is a one delegate being used for multiple table views, like when using a table view and a searchdisplaycontroller with a uisearchbar. Apples examples use the controller as the delegate, but reasoning still holds when using one custom delegate for both the main table view and the results table view for your search. That custom delegate would likely be retained by your controller in order to be provided to both table views.
This is fundamentally different from the basic delegation pattern that is referred to in other languages where the delegate is often created by the delegator and each instance may manage a its own delegate instance.
This is to avoid retain cycles. Apple offers an informative guide on advanced memory management explaining the situation and how best to deal with it. In ARC, they are now known as strong reference cycles, which are explained in the Transitioning to ARC Release Notes.
Previously you would define a property for a delegate like this,
@property (nonatomic, assign) id delegate;
But in ARC, you can define it thus,
@property (nonatomic, unsafe_unretained) id delegate;
Or, for example, if you have a protocol named
<MyObjectDelegate>, you can also define the delegate in this way,
@property (nonatomic, weak) id <MyObjectDelegate> delegate;
In other words, in ARC if you have a protocol, you can declare a delegate
As a common practice, if we have two objects holding references with each other, we make the “child” object in a “parent-children” relationship a weak reference.
For delegation patters in iOS, the delegated object is the parent, because there is no need for the delegate caller to exist without the delegated object. For example, you have a sentence object with delegate object for method sentenceShouldEnd. Your paragraph object is the delegated object for your sentence object. Obviously the paragraph object is actually the parent, and in your sentence object you should keep your delegate as a weak reference.
To your point you assign the delegate to self, your understanding is wrong. We should never assign delegate to itself. Why you buy your ticket yourself if you feel it is necessary to hire an agent to buy the ticket for you? You are saying two completely different concepts. When you define a delegate object as property, it’s used a weak reference in the object it is defined in(lets say A, i.e. the delegate object is a property of A). The delegate is assigned when you init A(let’s say in B), then most likely you would assign A.delegate to self, which is acturally B. You see the parent-child relationship here?? You alloc memory for A in B. You hold A in B. A does not exist without B. You are not assigning the delegate to A!!!!
- iVar property, access via self?
- Displaying Lots Of RichText : Choosing the best option
- UINavigationBar setBackgroundImage in AppDelegate with Swift
- Adding resource files to xcode
- How to correctly turn on and off SKEffectNode for a SKNode?
- iOS custom font path for UIWebViews
- Highlight active link in SWRevealViewController
- AVMutableComposition – Blank/Black frame between videos assets
- Swift countdown timer labels for Days/Hours/Minutes/Seconds
- How do you REALLY remove Copy from UIMenuController
- iPhone XCode – How to change title below app icon
- What is the AppDelegate for and how do I know when to use it?
- Longer subtitles in MapView annotations (swift)
- Custom clear button in UISearchBar text field
- Merging Xcode project files