iOS 处理循环引用的相关问题

2,610 阅读2分钟

循环引用常见原因

一、 当前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循环引用 20230331172004.jpg 解决方案:1.使用__weak typeof(self) weakSelf = self;打断循环引用

2.如果担心selfblock内提前释放,可用如下解决方式

@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

👉:保持良好的代码习惯很重要👌