RunLoop在實際開中的應(yīng)用
- 控制線程生命周期(線程保活);
- 解決NSTimer在滑動時停止工作的問題;
- 監(jiān)控應(yīng)用卡頓;
- 性能優(yōu)化;
1. 解決NSTimer在滑動時停止工作的問題
static int count = 0;
// 2.添加到指定模式下
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"%d", ++count);
}];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
// NSDefaultRunLoopMode、UITrackingRunLoopMode才是真正存在的模式
// NSRunLoopCommonModes并不是一個真的模式,它只是一個標(biāo)記
// timer能在_commonModes數(shù)組中存放的模式下工作
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
原因:是runloop只能運行在一種模式下,timer是在默認模式下工作的,不是在UItrackRunloop的模式下工作.
2. 線程笔嚎保活
我們先封裝一個長久活命的線程
\\PermanentThread.h
// 聲明一個block - 用于執(zhí)行任務(wù)
typedef void(^PermanentThreadTask)(void);
/** 線程泵┙活 */
@interface PermanentThread : NSObject
// 在當(dāng)前線程執(zhí)行一個任務(wù)
- (void)executeTask:(PermanentThreadTask)task;
// 結(jié)束線程
- (void)stop;
@end
\\PermanentThread.m
/** CSThread **/
@interface CSThread : NSThread
@end
@implementation CSThread
- (void)dealloc {
NSLog(@"%s", __func__);
}
@end
@interface PermanentThread()
/** 線程*/
@property(nonatomic,strong)CSThread *thread;
/** 是否停止*/
@property(nonatomic,assign, getter=isStopped)BOOL stopped;
@end
@implementation PermanentThread
// 初始化方法
- (instancetype)init {
self = [super init];
if (self) {
self.stopped = NO;
// 初始化線程
__weak typeof(self) weakSelf = self;
self.thread = [[CSThread alloc] initWithBlock:^{
// runloop只有添加事件才會執(zhí)行
[[NSRunLoop currentRunLoop] addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];
// 當(dāng)當(dāng)前對象存在并且變量為false的時候,才一直執(zhí)行
while (weakSelf && !weakSelf.isStopped) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}];
// 開啟線程
[self.thread start];
}
return self;
}
- (void)dealloc {
NSLog(@"%s", __func__);
[self stop];
}
#pragma mark - public method
// 執(zhí)行任務(wù)
- (void)executeTask:(PermanentThreadTask)task {
// 如果線程釋放或者無任務(wù),則退出
if (!self.thread || !task) {
return;
}
// 開始執(zhí)行任務(wù)
//waitUntilDone表示是否等待子線程處理完成后在運行之后的代碼.
[self performSelector:@selector(innerExecuteTask:) onThread:self.thread withObject:task waitUntilDone:NO];
}
// 停止
- (void)stop {
if (!self.thread) {
return;
}
[self performSelector:@selector(innerStop) onThread:self.thread withObject:nil waitUntilDone:YES];
}
#pragma mark - private method
// 執(zhí)行任務(wù)
- (void)innerExecuteTask:(PermanentThreadTask)task {
task();
}
// 停止線程 runloop
- (void)innerStop {
self.stopped = YES;
CFRunLoopStop(CFRunLoopGetCurrent());
self.thread = nil;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 2.線程卑校活
self.thread = [[PermanentThread alloc] init];
}
- (void)dealloc {
NSLog(@"%s", __func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.thread executeTask:^{
NSLog(@"執(zhí)行任務(wù) - %@", [NSThread currentThread]);
}];
}
- (void)stopBtnClick {
[self.thread stop];
}
想了解更多iOS學(xué)習(xí)知識請聯(lián)系:QQ(814299221)