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

2024-04-23 07:20
文章标签 应用 开发 ios runloop 实际

本文主要是介绍iOS - Runloop在实际开发中的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

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

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

这篇关于iOS - Runloop在实际开发中的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/928139

相关文章

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件

Python datetime 模块概述及应用场景

《Pythondatetime模块概述及应用场景》Python的datetime模块是标准库中用于处理日期和时间的核心模块,本文给大家介绍Pythondatetime模块概述及应用场景,感兴趣的朋... 目录一、python datetime 模块概述二、datetime 模块核心类解析三、日期时间格式化与

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验