文章目录
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]) {
s

本文介绍了iOS开发中如何使用Runloop进行线程控制,包括线程保活、解决NSTimer在滑动事件中暂停问题,以及监控应用卡顿和性能优化的方法。通过ZSXPermanenThread类实现线程的启动、任务执行和停止,以及使用NSRunLoop确保定时器在特定模式下持续运行。
最低0.47元/天 解锁文章
4409

被折叠的 条评论
为什么被折叠?



