WKWebView的这个循环引用很像定时器的循环引用。
定时器的循环引用是因为系统runloop循环 强引用了执行定时器方法的对象。
WKWebView,我认识是WebKit中有关js消息的某个系统 强引用了接收消息的对象。
我画个逻辑图如下:
既然知道原因了,我们可以像处理定时器一样,中间给它加一个弱引用,避开这种情况。
如下图所示:
2019年08月14日15:25:39补充
YYKit里面有个类YYWeakProxy,它利用代理机制与消息转发机制,真正的实现了这个中介者去弱引用的思想:
代码如下:
#import <Foundation/Foundation.h>
/**
A proxy used to hold a weak object.
It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
sample code:
@implementation MyView {
NSTimer *_timer;
}
- (void)initTimer {
YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
_timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
}
- (void)tick:(NSTimer *)timer {...}
@end
*/
@interface YYWeakProxy : NSProxy
/**
The proxy target.
*/
@property (nullable, nonatomic, weak, readonly) id target;
/**
Creates a new weak proxy for target.
@param target Target object.
@return A new proxy object.
*/
- (instancetype)initWithTarget:(id)target;
/**
Creates a new weak proxy for target.
@param target Target object.
@return A new proxy object.
*/
+ (instancetype)proxyWithTarget:(id)target;
@end
//
// YYWeakProxy.m
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 14/10/18.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "YYWeakProxy.h"
/**
实现的原理: 使用 NSProxy 持有 NSTimer 的 target
不再用 NSTimer 直接持有 self,就不会导致 timer 对 self 的循环强引用了
*/
@implementation YYWeakProxy
- (instancetype)initWithTarget:(id)target {
_target = target;
return self;
}
//类方法
+ (instancetype)proxyWithTarget:(id)target {
return [[YYWeakProxy alloc] initWithTarget:target];
}
#pragma mark - private
// 在处理消息转发时,将消息转发给真正的Target处理
- (id)forwardingTargetForSelector:(SEL)selector {
return _target;
}
#pragma mark - over write
//重写NSProxy如下两个方法,保证代理执行时不会报错
// 方法签名,甭管传入什么方法,都是转成NSObject的init的方法签名
//这个函数本意是让重载方有机会抛出一个函数的签名,再由后面的forwardInvocation:去执行为给定消息提供参数类型信息
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}
//这里设计很巧妙,一般人是这样调用的 [anInvocation invokeWithTarget:self.obj];
//但是作者只修改了返回值为null,并不调用方法。因为真正的方法调用根本不在这里,他在- (id)forwardingTargetForSelector:(SEL)selector这里就是实现了调用。
//所以郭老师复写两个方法,目的只有一个:避免抛出异常:unrecognized selector sent to instance
- (void)forwardInvocation:(NSInvocation *)invocation {
void *null = NULL;
[invocation setReturnValue:&null];
}
#pragma mark - <NSObject>
// 重写NSObject.h定义的方法
//这个方法特殊,它返回NO,就都不会执行了
- (BOOL)respondsToSelector:(SEL)aSelector {
return [_target respondsToSelector:aSelector];
}
- (BOOL)isEqual:(id)object {
return [_target isEqual:object];
}
- (NSUInteger)hash {
return [_target hash];
}
- (Class)superclass {
return [_target superclass];
}
- (Class)class {
return [_target class];
}
- (BOOL)isKindOfClass:(Class)aClass {
return [_target isKindOfClass:aClass];
}
- (BOOL)isMemberOfClass:(Class)aClass {
return [_target isMemberOfClass:aClass];
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol {
return [_target conformsToProtocol:aProtocol];
}
- (BOOL)isProxy {
return YES;
}
- (NSString *)description {
return [_target description];
}
- (NSString *)debugDescription {
return [_target debugDescription];
}
@end
参考文章:1.关于NSProxy的理解
2.利用消息转发机制屏蔽unrecognized selector sent to instance