ios之runloop笔记
网上关于runloop的文章不计其数,再此,贴个自认为讲的比较简单明了的文章
个人理解:
ios的runloop应该是类似于线程之间的消息监听+队列(队列于外部不透明,支持多重send消息模式,perform selector,timer,UI事件等等)
和android的Looper非常相似,和windows的消息循环也很类似,具体底层实现不关注,直接贴测试代码
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong) NSThread *thread;
@property(nonatomic, strong) NSThread *msgThread; @end @implementation ViewController - (void) viewDidLoad{
[super viewDidLoad]; [self initMsgThread];
} - (void) initMsgThread{ self.msgThread = [[NSThread alloc]initWithTarget:self selector:@selector(msgThreadInitFunc) object:nil];
[self.msgThread start];
} - (void) msgThreadInitFunc{
NSLog(@"msgThreadInitFunc run, %@",[NSThread currentThread]); [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run]; NSLog(@"msgThreadInitFunc run"); } //******************************************************************************************************************
- (void) postMsgToThread{
[[[NSThread alloc]initWithTarget:self selector:@selector(asyncPostMsgToThread) object:nil] start];
} - (void) asyncPostMsgToThread{
[self performSelector:@selector(onThreadMsgProc) onThread:self.msgThread withObject:nil waitUntilDone:NO];
} - (void) onThreadMsgProc{
NSLog(@"do some work %@, %s", [NSThread currentThread], __FUNCTION__);
} //******************************************************************************************************************
- (void) startTimerOnThread{
self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(asyncStartTimerOnThread) object:nil];
[self.thread start];
} - (void) asyncStartTimerOnThread{
[self performSelector:@selector(asyncStartTimer) onThread:self.thread withObject:nil waitUntilDone:NO]; // [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
} - (void) asyncStartTimer{
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerFired:)userInfo:nil repeats:YES];
} - (void) timerFired:(id)timer{
NSLog(@"on thread timer %s",__FUNCTION__);
} //******************************************************************************************************************
- (IBAction)testButtonTapped:(id)sender {
// [self postMsgToThread];
[self startTimerOnThread];
} @end
当然用block也是一样的,子线程必须创建runloop来监听消息,否则这个子线程是无法处理类似performSelector,NSTimer之类的消息的
线程之间通信,cocos2dx,u3d,ios,android,win32,都是基于消息队列的模式,一个发,一个收,写时加锁,别无更好的办法了
ios之runloop笔记的更多相关文章
- iOS 字符串处理笔记
iOS字符串处理笔记,包括如何使用正则表达式解析,NSScanner扫描,设置和使用CoreParse解析器来解析处理自定义符号等内容 搜索 在一个字符串中搜索子字符串 最灵活的方法 - (NSRan ...
- iOS多线程-RunLoop简介
什么是RunLoop? 从字面上来看是运行循环的意思. 内部就是一个do{}while循环,在这个循环里内部不断的处理各种任务(比如:source/timer/Observer) RunLoop的存在 ...
- RunLoop笔记
1.Runloop基础知识 - 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如 ...
- iOS - OC RunLoop 运行循环/消息循环
1.RunLoop 1)运行循环: 运行循环在 iOS 开发中几乎不用,但是概念的理解却非常重要. 同一个方法中的代码一般都在同一个运行循环中执行,运行循环监听 UI 界面的修改事件,待本次运行循环结 ...
- IOS懒人笔记应用源码
这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...
- iOS关于RunLoop和Timer
RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在.RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop.永远不要手动创建一个run ...
- iOS开发RunLoop
最近处于离职状态,时间也多了起来,但是学习还是不能放松,今天总结一下RunLoop,RunLoop属于iOS系统层的东西,还是比较重要的. 一.什么是RunLoop 字面意思看是跑圈,也可以看作运行循 ...
- iOS之RunLoop
RunLoop是iOS线程相关的比较重要的一个概念,无论是主线程还是子线程,都对应一个RunLoop,如果没有RunLoop,线程会马上被系统回收. 本文主要CFRunLoop的源码解析,并简单阐述一 ...
- iOS开发-Runloop详解(简书)
不知道大家有没有想过这个问题,一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件 ...
随机推荐
- linux shard virtual memory
- SQL 查询重复的行
select * from tbsold where orderid in (select orderid from tbsold group by orderid having count(orde ...
- 所谓的规范以及JDK api文档的重要性
所谓的规范,就是在jee api 文档里对应的接口. 可以从jdk文档和jee文档的目录结构,接口中获取对整个编程范围的把握
- js常用用途
页面元素查找 查找元素方法表达式 返回值 依据元 ...
- 软工作业No.9 第六周 事后诸葛亮分析报告
甜美女孩项目2048结果 整理:邓画月.曾祎祺 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 弄一个给用户消磨时间的游戏,定义的很清楚.该游戏玩 ...
- MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(三)
MyEclipse超值折扣 限量 100 套! 立即开抢>> [MyEclipse最新版下载] MyEclipse支持Java EE技术(如JAX-WS和EJB 3.0),它们以功能包的形 ...
- DevExpress WPF入门指南:绑定编辑器对话框
绑定编辑器对话框 每个Smart Tag属性既可以设置也可以绑定.如下图所示,点击绑定按钮打开绑定对话框: 如果属性已经绑定,binging按钮会显示为黄色,绑定的文本会显示在相应的属性行. 绑定So ...
- SharePoint Web应用程序管理-PowerShell
1. 显示场中的Web应用程序 Get-SPWebApplication 2. 获取指定的Web应用程序 $webApp = Get-SPWebApplication -Identity " ...
- Tomcat服务器启动失败:Could not publish server configuration for Tomcat v8.0 Server at localhost. Multiple Contexts have a path of
在eclipse中使用Tomcat8.0时,出现了如下错误: 解决办法: 在建立Tomcat服务时,eclipse会自动生成一个Servers的项目. 在这个项目中,找到你部署项目的服务文件夹. 在这 ...
- DIY微型操作系统(2)—— 寄存器赋值
书上已经为我们提供了最简单的“hello world”程序了 相信大家玩的也挺欢乐的,不过这只是引导扇区的雏形 而且如果找不到完整的光盘上的源代码,新手估计很难学的下去 所以之后文中也会尽量贴出代码 ...