Recent development encountered a problem, in the cell opened the Cadisplaylink and joined the Runloop, in the release, VC and other cell dealloc are gone, only open the timer cell does not go Dealloc method.
The view found that nstimer against Target was a strong reference to the target leak, to resolve this problem must break the circular reference, in the middle of Nstimer and target more than one layer of proxy (proxy), agent as target by Nstimer strong reference, It also weakly references the real target and forwards the message to it.
Nsproxy is early to understand but not really used, this time just practice.
Before using Nsproxy, simply review the runtime message forwarding mechanism:
The first step, dynamic method parsing
+ (BOOL) Resolveinstancemethod: (SEL) sel;
+ (BOOL) Resolveclassmethod: (SEL) sel;
Second step, standby receiver
-(ID) Forwardingtargetforselector: (SEL) Selector
If an object implements this method and returns a nil result, the object is used as the new recipient of the message, and the message is distributed to the object. Of course this object cannot be self itself, otherwise there is an infinite loop. Of course, if we do not specify an object to handle aselector, we should call the implementation of the parent class to return the result.
Step three, full message forwarding
-(void) Forwardinvocation: (nsinvocation *) invocation;
In this step, the runtime system gives the message receiver the last chance to forward the message to another object. Object creates a Nsinvocation object that represents the message, encapsulating all the details about the message that have not been processed in the aninvocation, including selector, Target (target), and parameters. We can choose to forward messages to other objects in the Forwardinvocation method.
-(Nullable Nsmethodsignature *) Methodsignatureforselector: (SEL) The SEL message forwarding mechanism uses the information obtained from this method to create the Nsinvocation object. So we have to rewrite this method to provide a proper method signature for the given selector.
Our proxy is in the second step, the target as the message receiver response method. The specific code can refer to yy great God's Yyweakproxy
Https://github.com/yehot/YYFPSLabel/blob/master/YYFPSLabel/YYFPSLabel/YYWeakProxy.m