CFRunLoop学习
from CFRunLoop Reference
CFRunLoop monitors sources of input to a task and dispatches control when they become ready for processing.
Examples of input source might include user input devices,network connection,periodic or timed-delay events,and asynchronous callbacks.
Three types of objects can be monitored by a run loop:
sources(CFRunLoopSource)
timers(CFRunLoopTimer)
observers(CFRunLoopObserver)
Each sources,times or observers added to a run loop must be associated with one or more run loop modes.
There is exactly one run loop per thread.
RunLoop 是事件循环,用于schedule work 和协调输入事件.
RunLoop 接受2种不同的事件源,input sources (异步事件)和 timer sources(同步事件)
何时使用RunLoop?
1.使用ports或自定义输入源 与其他线程通讯
2.在线程中使用定时器
3.在程序中使用performSelector等方法
4.让线程周期性执行某个任务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步