-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Description
- Runloop 在运行之前必须指定一个Mode
- Mode 底层数据结构如下所示:
struct __CFRunLoopMode {
CFRuntimeBase _base;
pthread_mutex_t _lock; /* 一个mutex,锁mode里的各种操作。根据注释,需要runloop 的锁先锁上才能锁这个锁。 */
CFStringRef _name; /* 模式名称 */
Boolean _stopped; //是否停止了
char _padding[3];
CFMutableSetRef _sources0; //Source0 集合,也就是非 port 的 source
CFMutableSetRef _sources1; //Source1 集合,也就是基于 port 的 source
CFMutableArrayRef _observers; //Observer 集合
CFMutableArrayRef _timers; //Timer 集合
CFMutableDictionaryRef _portToV1SourceMap; //Key 是 port,value 是对应 source1 的字典
__CFPortSet _portSet; //所有 port 的集合
CFIndex _observerMask; //需要 observe 的事件的 mask
#if USE_DISPATCH_SOURCE_FOR_TIMERS
dispatch_source_t _timerSource; //用来实现 timer 的 GCD timer
dispatch_queue_t _queue; //放 _timerSource 的队列
Boolean _timerFired; // _timerSource 是否被启动
Boolean _dispatchTimerArmed; //timer是否开启了
#endif
#if USE_MK_TIMER_TOO
mach_port_t _timerPort; //使用 MK timer 时的端口
Boolean _mkTimerArmed; //timer 是否被开启
#endif
#if DEPLOYMENT_TARGET_WINDOWS
DWORD _msgQMask;
void (*_msgPump)(void);
#endif
uint64_t _timerSoftDeadline; /* //下一个计划启动的时间 */
uint64_t _timerHardDeadline; /* 下一个最迟启动的时间(计划加上容忍延迟的时间)*/
};每个Mode里面存放了当前模式下的****_sources0****,_sources1,_observers,_timers,_portSet
- iOS 系统中定义了如下几种Mode:
-
NSDefaultRunLoopMode:
NSDefaultRunLoopMode是Runloop的默认的运行模式,主线程在启动的时候就是运行在这个模式下的,NSTimer与NSURLConnection默认运行该模式下。 -
UITrackingRunLoopMode:在触摸滑动界面的时候会切换到这个模式用于保证界面滑动时不受其他 Mode 影响
-
UIInitializationRunLoopMode:在刚启动 App 时第进入的第一个 Mode,启动完成后就不再使用。
-
NSRunLoopCommonModes:这是一个 Mode 的集合。注册到这个 Mode 下后,无论当前 runLoop 运行哪个 mode ,事件都能得到执行。默认包括 NSDefaultRunLoopMode 和 NSEventTrackingRunLoopMode
-
Metadata
Metadata
Assignees
Labels
No labels