当前位置:网站首页>Cadisplaylink, nstimer circular reference solution
Cadisplaylink, nstimer circular reference solution
2022-07-21 02:36:00 【nginx】
CADisplayLink、NSTimer Would be right Target Generate strong quotes , If target They are strongly quoted again , Then it will trigger a circular reference .
__weak typeof(self) weakSelf = self; Can we solve it NSTimer The problem of circular quotation ?
@interface ViewController ()
@property (nonatomic, strong) CADisplayLink *link;
@property (nonatomic, strong) NSTimer *time;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Ensure the calling frequency and frame brushing frequency 60fps
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkTest)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode] ;
self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeTest) userInfo:nil repeats:YES];
}
- (void)timeTest {
NSLog(@"%s", __func__);
}
- (void)linkTest {
NSLog(@"%s", __func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%s", __func__);
[self.time invalidate];
}
answer : We cannot pass __weak typeof(self) weakSelf = self; Code to solve circular references .
__weak typeof(self) weakSelf = self; It's for block Can solve the problem of circular reference .
When
Scheme 1 、 Use NSTimer Use block(CADisplayLink Not provided block api You can't use )
NSTime Use to provide block Of API , adopt __weak Solve the problem of sequential reference
Option two 、 Use NSObject forward
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timeTest];
} ];
}
- (void)timeTest {
NSLog(@"%s", __func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%s", __func__);
[self.time invalidate];
}
Through intermediate variables , It is a reference in the middle that becomes a weak reference .
First step : Create inheritance NSObject Of RHMiddleTarget Intermediate document , establish target attribute , Use weak To modify by weak reference
The second step : In the use of CADisplayLink and NSTimer Medium target Pass it to the intermediate variable :
#import <Foundation/Foundation.h>
@interface RHMiddleTarget : NSObject
@property (nonatomic, weak) id target;
+ (instancetype)middleTargetWithTarget:(id)target;
@end
#import "RHMiddleTarget.h"
@implementation RHMiddleTarget
+ (instancetype)middleTargetWithTarget:(id)target {
RHMiddleTarget *middleTarget = [[RHMiddleTarget alloc] init];
middleTarget.target = target;
return middleTarget;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
// Return the object that can be processed , Send... To the object aSelector news
// objc_msgSend(self.target, aSelector);
return self.target;
}
@end
The third step : Test verification :
// Ensure the calling frequency and frame brushing frequency 60fps
self.link = [CADisplayLink displayLinkWithTarget:[RHMiddleTarget middleTargetWithTarget:self] selector:@selector(linkTest)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode] ;
self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[RHMiddleTarget middleTargetWithTarget:self] selector:@selector(timeTest) userInfo:nil repeats:YES];
Option three 、 Use NSProxy forward
2022-07-05 16:01:47.895716+0800 Interview03- Timer [10828:260303] -[ViewController timeTest]
2022-07-05 16:01:48.895649+0800 Interview03- Timer [10828:260303] -[ViewController timeTest]
2022-07-05 16:01:49.895571+0800 Interview03- Timer [10828:260303] -[ViewController timeTest]
2022-07-05 16:01:50.666056+0800 Interview03- Timer [10828:260303] -[ViewController touchesBegan:withEvent:]
First step : Create inheritance NSObject Of RHMiddleTarget Intermediate document , establish target attribute , Use weak To modify by weak reference
The second step : In the use of CADisplayLink and NSTimer Medium target Pass it to the intermediate variable
#import <Foundation/Foundation.h>
@interface RHMiddleTarget : NSProxy
@property (nonatomic, weak) id target;
+ (instancetype)middleTargetWithTarget:(id)target;
@end
#import "RHMiddleTarget.h"
@implementation RHMiddleTarget
+ (instancetype)middleTargetWithTarget:(id)target {
RHMiddleTarget *middleTarget = [RHMiddleTarget alloc];
middleTarget.target = target;
return middleTarget;
}
// Return method signature
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.target methodSignatureForSelector:sel];
}
// Make the corresponding call
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:self.target];
}
The third step : Test verification
// Ensure the calling frequency and frame brushing frequency 60fps
self.link = [CADisplayLink displayLinkWithTarget:[RHMiddleTarget middleTargetWithTarget:self] selector:@selector(linkTest)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode] ;
self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[RHMiddleTarget middleTargetWithTarget:self] selector:@selector(timeTest) userInfo:nil repeats:YES];
Option four 、 Use viewWillDisappear
2022-07-05 16:27:25.427454+0800 Interview03- Timer [11550:281823] -[ViewController linkTest]
2022-07-05 16:27:25.444101+0800 Interview03- Timer [11550:281823] -[ViewController linkTest]
2022-07-05 16:27:25.456983+0800 Interview03- Timer [11550:281823] -[ViewController touchesBegan:withEvent:]
When the controller is destroyed or the timer stops , Call the following method
[self.timer invalidate];
[self.link invalidate];
self.timer = nil;
self.link = nil;
边栏推荐
- 《暴走 IT》第1话:来了个新同事
- DiFi: A Go-as-You-Pay Wi-Fi Access System 精读笔记(二)
- Mobile phone remedial classes for parents
- China's first white paper on the application of electronic signatures in the construction industry was released
- TCP协议的相关特性
- typora测试版过期无法正常使用
- 百家云与美股上市公司富维薄膜达成合并协议,最快于下半年完成合并
- 计算从今天开始的几天前的日期
- Why is the data found in the database partially lost
- 独立站卖家如何利用Facebook主页进行社交媒体营销?
猜你喜欢
Advanced one stage of target detection
PPT柱状图如何增加柱子
Kingdee's "answer sheet" forecast in the first half of the year: key customer strategy continued to break through, and arr increased by more than 45% year-on-year
How do independent website sellers use Facebook homepage for social media marketing?
Several common models of convolutional neural network CNN
Binary tree OJ exercise
Trax: how to win the sweet war in the summer ice cream market?
Difi: a go as you pay Wi Fi access system intensive reading notes (II)
Excel宏是什么?Excel宏的使用教程
【MUDUO SOCKET】InetAddress 封装SOCKET地址类型
随机推荐
Alibaba cloud international account registration FAQ summary
Analysis on the wallet system architecture of Baidu trading platform
容量调度绝对值配置队列使用与避坑
Relationship between accuracy, recall and confidence
Binary tree OJ exercise
Get the way to optimize the one-stop worktable of customer service
【MUDUO 日志系统2】Timestamp时间
密码密钥硬编码检查
中国首部《建筑行业电子签名应用白皮书》发布
A detailed explanation of the implementation principle of go Distributed Link Tracking
jdbc error code
计算从今天开始的几天前的日期
一文详解|Go 分布式链路追踪实现原理
apple 为什么要改 objc
[pytoch] tensorboard usage: scalar curve, histogram, model structure diagram
Returns the quarter and year of the date provided
Date range generator
青海vr消防模拟演练系统,满足了对多人群多场景下的培训需求
检查日期是否在其他两个日期之间
Difference between BN and LN