循环引用常见原因:
一、 当前VC
使用了NSTimer
, 并没有对它进行销毁.
Tips:在计时结束或者离开页面的时候,销毁timer
,步骤如下:
if(_timer) {
[_timer invalidate];
_timer = nil;
}
二、 block
块内使用了self
,造成了循环引用.
Tips1:一般的系统方法或者类方法不会造成循环引用,并不是所有的
block
都会造成循环引用,一般只有实例方法的block
块内,调用了self
的方法才会造成循环引用
Tips2:调用了
performSelector
延迟方法的,dealloc
方法会延迟执行 利用如下函数可以解决:[NSObject cancelPreviousPerformRequestsWithTarget:self];
或[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method1:) object:nil];
Tips3: 常见自定义
cell
里面的block循环引用解决方案:1.使用
__weak typeof(self) weakSelf = self;
打断循环引用2.如果担心
self
在block
内提前释放,可用如下解决方式
@weakify(self);
cell.didTag = ^(NSInteger tag) {
@strongify(self);
[self didJianGuanTag:tag];
};
三、使用delegate
, 用了strong
修饰,要使用weak
来修饰.
@property(nonatomic, weak) id<WEBidListTableViewCellDelegate>delegate;
四、NSNotificationCenter
使用 block
注册addObserver(forName:object:queue:using:)
,但是没有在dealloc中移除;
如果在block内使用了self,就要打破循环引用:
@weakify(self);
[[NSNotificationCenter defaultCenter]addObserverForName:@"RefreshMySelectImg" object:nil queue:nil usingBlock:^(NSNotification *_Nonnull note) {
@strongify(self);
[self.tableView reloadData];
}];
五、vc
中使用了WKWebView
调用addScriptMessageHandler
造成了循环引用.
前言结论:在与H5进行交互时,经常使用
addScriptMessageHandler
添加参数信息,它需要添加一个userContentController
代理对象;self
强引用- >WKWebView
- >configuration
参数 ->userContentController
2.userContentController
强引用 - >self
解决方案1.在viewWillAppear
方法中添加 addScriptMessageHandler
,在viewWillDisappear
方法中移除调用
解决方案2. 打断循环引用,具体如下调用使用
- (void)addJSHandler {
id webViewSelf = [[WeakScriptMessageDelegate alloc] initWithDelegate:self];
[self.webView.configuration.userContentController addScriptMessageHandler:webViewSelf name:@"js_close"];
}
// WeakScriptMessageDelegate.h
#import <Foundation/Foundation.h>
@interface WeakScriptMessageDelegate : NSObject <WKScriptMessageHandler>
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
// WeakScriptMessageDelegate.m
#import "WeakScriptMessageDelegate.h"
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end
👉:保持良好的代码习惯很重要👌