iOS - Runloop在实际开发中的应用

ops/2024/10/18 18:26:31/

文章目录

  • iOS - Runloop在实际开发中的应用
    • 1. 控制线程生命周期(线程保活)
    • 2. 解决NSTimer在滑动时停止工作的问题
      • 2.1. 案例
      • 2.2 解决
    • 3. 监控应用卡顿
    • 4. 性能优化

iOS__Runloop_1">iOS - Runloop在实际开发中的应用

1. 控制线程生命周期(线程保活)

如果需要经常在子程序执行任务,可能希望一个线程可以重复使用,避免每次都要创建、销毁带来不必要的开销

ZSXPermenantThread.h

typedef void (^ZSXPermenantThreadTask)(void);@interface ZSXPermenantThread : NSObject/**开启线程*/
//- (void)run;/**在当前子线程执行一个任务*/
- (void)executeTask:(ZSXPermenantThreadTask)task;/**结束线程*/
- (void)stop;@end

ZSXPermenantThread.m

#import "ZSXPermenantThread.h"/** ZSXThread **/
@interface ZSXThread : NSThread
@end
@implementation ZSXThread
- (void)dealloc
{NSLog(@"%s", __func__);
}
@end/** ZSXPermenantThread **/
@interface ZSXPermenantThread()@property (strong, nonatomic) ZSXThread *innerThread;
@property (assign, nonatomic, getter=isStopped) BOOL stopped;@end@implementation ZSXPermenantThread#pragma mark - public methods
- (instancetype)init {if (self = [super init]) {self.stopped = NO;__weak typeof(self) weakSelf = self;self.innerThread = [[ZSXThread alloc] initWithBlock:^{[[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];while (weakSelf && !weakSelf.isStopped) {[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];}}];[self.innerThread start];}return self;
}- (void)executeTask:(ZSXPermenantThreadTask)task {if (!self.innerThread || !task) return;[self performSelector:@selector(__executeTask:) onThread:self.innerThread withObject:task waitUntilDone:NO];
}- (void)stop {if (!self.innerThread) return;[self performSelector:@selector(__stop) onThread:self.innerThread withObject:nil waitUntilDone:YES];
}- (void)dealloc {NSLog(@"%s", __func__);[self stop];
}#pragma mark - private methods
- (void)__stop {self.stopped = YES;CFRunLoopStop(CFRunLoopGetCurrent());self.innerThread = nil;
}- (void)__executeTask:(ZSXPermenantThreadTask)task {task();
}@end

在 ViewController中使用

@interface ViewController ()@property (strong, nonatomic) ZSXPermenantThread *thread;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.thread = [[ZSXPermenantThread alloc] init];
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{[self.thread executeTask:^{NSLog(@"执行任务 - %@", [NSThread currentThread]);}];
}- (IBAction)stop:(UIButton *)sender {[self.thread stop];
}- (void)dealloc
{NSLog(@"%s", __func__);
}@end

运行结果:

2. 解决NSTimer在滑动时停止工作的问题

2.1. 案例

使用NSTimer创建一个定时器,循环打印

[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {NSLog(@"%d", ++count);
}];

当scrollView 滚动时,定时器就停止了

2.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 并不是一个真的模式,它只是一个标记而已
// time 能在_commonModes数组中存放的的模式下工作。也就是包含NSDefaultRunLoopMode、UITrackingRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

3. 监控应用卡顿

4. 性能优化

@oubijiexi


http://www.ppmy.cn/ops/9977.html

相关文章

开源啦!一键部署免费使用!Kubernetes上直接运行大数据平台!

市场上首个K8s上的大数据平台&#xff0c;开源啦&#xff01; 智领云自主研发的首个 完全基于Kubernetes的容器化大数据平台 Kubernetes Data Platform (简称KDP) 开源啦&#x1f680;&#x1f680; 开发者只要准备好命令行工具&#xff0c;一键部署 Hadoop&#xff0c;Hi…

【前端】node.js常用命令

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、node是什么二、node.js常用命令三、总结 前言 随着开发语言及人工智能工具的普及&#xff0c;使得越来越多的人能够上手操作执行一些简单命令&#xff0c;…

milvus querynode启动源码分析

querynode启动源码分析 结构体 // QueryNode implements QueryNode grpc server // cmd\components\query_node.go type QueryNode struct {ctx context.Contextsvr *grpcquerynode.Server }// Server is the grpc server of QueryNode. type Server struct {querynode typ…

怎么通过Javascript脚本实现远程控制一路开关

怎么通过Javascript脚本实现远程控制一路开关呢&#xff1f; 本文描述了使用Javascript脚本调用HTTP接口&#xff0c;实现控制一路开关。一路开关可控制一路照明、排风扇等电器。 可选用产品&#xff1a;可根据实际场景需求&#xff0c;选择对应的规格 序号设备名称1智能WiFi…

3D开发工具HOOPS助力CAM软件优化制造流程

在现代制造业中&#xff0c;计算机辅助制造&#xff08;CAM&#xff09;软件的发展已成为提高生产效率和产品质量的关键。为了满足不断增长的需求和日益复杂的制造流程&#xff0c;CAM软件需要具备高效的CAD数据导入、云端协作、移动应用支持以及丰富的文档生成能力。 Tech So…

【Ansible】04

【Ansible】03 任务块 block任务块 使用 block 可以将多个任务合并为一个组可以将整个 block任务组 , 一起控制是否要执行 # 如果webservers组中的主机系统发行版是Rocky&#xff0c;则安装并启动nginx [rootpubserver ansible]# vim block1.yml --- - name: block tasksho…

微软刚开源就删库的WizardLM-2:MT-Bench 榜单评测超越GPT-4,7B追平Qwen1.5-32B

前言 微软最近发布的WizardLM-2大型语言模型因其先进的技术规格和短暂的开源后突然撤回&#xff0c;引起了科技界的广泛关注。WizardLM-2包括三个不同规模的模型&#xff0c;分别是8x22B、70B和7B&#xff0c;均展现了在多语言处理、复杂对话、推理和代理任务上的卓越能力。 H…

深度神经网络(DNN)

通过5个条件判定一件事情是否会发生&#xff0c;5个条件对这件事情是否发生的影响力不同&#xff0c;计算每个条件对这件事情发生的影响力多大&#xff0c;写一个深度神经网络&#xff08;DNN&#xff09;模型程序,最后打印5个条件分别的影响力。 示例 在深度神经网络&#xf…