RunLoop解析

什么是RunLoop?

顾名思义:
运行循环
在程序运行过程中循环做一些事情


应用范畴:
定时器(Timer)、PerformSelector
GCD Async Main Queue
事件响应、手势识别、界面刷新
网络请求
AutoreleasePool

如果没有RunLoop

int main(int argc, char * argv[]) {
    @autoreleasepool {
    }
    return 0;
}

UIApplicationMain 函数执行完毕之后将直接返回,就是说程序一启动然后就结束

如果有了RunLoop


int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
*****************************
伪代码
*****************************
int main(int argc, char * argv[]) {
    @autoreleasepool {
        int retVal = 0;
        do {
            //睡眠中等待消息
            int message = sleep_and_wait();
            //处理消息
            retVal = process_message(message);
        }while (0 == retVal);
        return  0;
    }
}
  • 程序并不会马上退出,而是保持运行状态
  • RunLoop的基本作用
    保持程序的持续运行
    处理App中的各种事件(比如触摸事件、定时器事件等)
    节省CPU资源,提高程序性能:该做事时做事,该休息时休息
    ......

RunLoop对象

苹果官方文档
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

  • iOS中有2套API来访问和使用RunLoop
    Foundation:NSRunLoop
    Core Foundation:CFRunLoopRef

  • NSRunLoop和CFRunLoopRef都代表着RunLoop对象
    NSRunLoop是基于CFRunLoopRef的一层OC包装
    CFRunLoopRef是开源的
    https://opensource.apple.com/tarballs/CF/

RunLoop与线程的关系

static CFMutableDictionaryRef __CFRunLoops = NULL;

CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {
  ....
 if (!__CFRunLoops) {
****************************
主线程的RunLoop
****************************
    CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
    CFRunLoopRef mainLoop = __CFRunLoopCreate(pthread_main_thread_np());
    CFDictionarySetValue(dict, pthreadPointer(pthread_main_thread_np()), mainLoop);
    }
...
****************************
根据线程key获取loop对象
****************************
    CFRunLoopRef loop = (CFRunLoopRef)CFDictionaryGetValue(__CFRunLoops, pthreadPointer(t));
    __CFUnlock(&loopsLock);
    if (!loop) {
****************************
没有找到loop对象就创建新的newLoop
****************************
    CFRunLoopRef newLoop = __CFRunLoopCreate(t);
        __CFLock(&loopsLock);
    loop = (CFRunLoopRef)CFDictionaryGetValue(__CFRunLoops, pthreadPointer(t));
    if (!loop) {
****************************
保存newLoop到__CFRunLoops的全局字典中
****************************
        CFDictionarySetValue(__CFRunLoops, pthreadPointer(t), newLoop);
        loop = newLoop;
    }
     ...
    }
   ....
    return loop;
}

1.每条线程都有唯一的一个与之对应的RunLoop对象。
2.RunLoop保存在一个全局的Dictionary里,线程作为key,RunLoop作为value
3.RunLoop在第一次获取时创建,在线程结束时销毁。
4.主线程的RunLoop已经自动获取(创建),子线程默认没有开启RunLoop。

获取RunLoop对象

  • Foundation
[NSRunLoop currentRunLoop]; // 获得当前线程的RunLoop对象
[NSRunLoop mainRunLoop]; // 获得主线程的RunLoop对象
  • Core Foundation
CFRunLoopGetCurrent(); // 获得当前线程的RunLoop对象
CFRunLoopGetMain(); // 获得主线程的RunLoop对象

RunLoop相关的类

struct __CFRunLoop {
   ...
    pthread_t _pthread;
    CFMutableSetRef _commonModes;
    CFMutableSetRef _commonModeItems;
    CFRunLoopModeRef _currentMode;
    CFMutableSetRef _modes;
   ....
};
struct __CFRunLoopMode {
...
    CFStringRef _name;
    CFMutableSetRef _sources0;
    CFMutableSetRef _sources1;
    CFMutableArrayRef _observers;
    CFMutableArrayRef _timers;
....
};


CFRunLoopModeRef

  • CFRunLoopModeRef代表RunLoop的运行模式
  • 一个RunLoop包含若干个Mode,每个Mode又包含若干个Source0/Source1/Timer/Observer
  • RunLoop启动时只能选择其中一个Mode,作为currentMode
  • 如果需要切换Mode,只能退出当前Loop,再重新选择一个Mode进入
    不同组的Source0/Source1/Timer/Observer能分隔开来,互不影响
  • 如果Mode里没有任何Source0/Source1/Timer/Observer,RunLoop会立马退出

CFRunLoopModeRef
kCFRunLoopDefaultMode:App的默认Mode,通常主线程是在这个Mode下运行
UITrackingRunLoopMode:界面跟踪 Mode,用于 ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响
UIInitializationRunLoopMode: 在刚启动 App 时第进入的第一个 Mode,启动完成后就不再使用
GSEventReceiveRunLoopMode: 接受系统事件的内部 Mode,通常用不到
kCFRunLoopCommonModes: 这是一个占位用的Mode,不是一种真正的Mode

CFRunLoopObserverRef
CFRunLoopObserverRef是观察者,能够监听RunLoop的状态改变。

/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
    kCFRunLoopEntry = (1UL << 0),                 即将进入loop
    kCFRunLoopBeforeTimers = (1UL << 1),          即将处理Timer
    kCFRunLoopBeforeSources = (1UL << 2),         即将处理Source
    kCFRunLoopBeforeWaiting = (1UL << 5),         即将进入休眠
    kCFRunLoopAfterWaiting = (1UL << 6),          刚从休眠中唤醒
    kCFRunLoopExit = (1UL << 7),                  即将退出loop
    kCFRunLoopAllActivities = 0x0FFFFFFFU
};

示例

@interface ViewController ()
@end

@implementation ViewController
void observeRunLoopActicities(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
{
    switch (activity) {
        case kCFRunLoopEntry:
            NSLog(@"kCFRunLoopEntry");
            break;
        case kCFRunLoopBeforeTimers:
            NSLog(@"kCFRunLoopBeforeTimers");
            break;
        case kCFRunLoopBeforeSources:
            NSLog(@"kCFRunLoopBeforeSources");
            break;
        case kCFRunLoopBeforeWaiting:
            NSLog(@"kCFRunLoopBeforeWaiting");
            break;
        case kCFRunLoopAfterWaiting:
            NSLog(@"kCFRunLoopAfterWaiting");
            break;
        case kCFRunLoopExit:
            NSLog(@"kCFRunLoopExit");
            break;
        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
     //创建Observer
    CFRunLoopObserverRef observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, observeRunLoopActicities, NULL);
    // 添加Observer到RunLoop中
    CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes);
    // 释放
    CFRelease(observer);
}

kCFRunLoopCommonModes默认包括kCFRunLoopDefaultMode、UITrackingRunLoopMode

RunLoop的运行逻辑

  • Source0
    触摸事件处理
    performSelector:onThread:

  • Source1
    基于Port的线程间通信
    系统事件捕捉

  • Timers
    NSTimer
    performSelector:withObject:afterDelay:

  • Observers
    用于监听RunLoop的状态
    UI刷新(BeforeWaiting)
    Autorelease pool(BeforeWaiting)

RunLoop源码分析

如下方法中设置断点,然后点击设备屏幕

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}

查看堆栈调用信息

(lldb) bt
  ...
    frame #10: 0x00000001bb51c958 CoreFoundation`__CFRunLoopDoSource0 + 80
    frame #11: 0x00000001bb51c0f0 CoreFoundation`__CFRunLoopDoSources0 + 180
    frame #12: 0x00000001bb51723c CoreFoundation`__CFRunLoopRun + 1080
    frame #13: 0x00000001bb516adc CoreFoundation`CFRunLoopRunSpecific + 464
    ...
(lldb) 

可以看到RunLoop是从CFRunLoopRunSpecific函数开始的

SInt32 CFRunLoopRunSpecific(CFRunLoopRef rl, CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled) {     /* DOES CALLOUT */
...
****************************
通知Observers : 进入RunLoop
****************************
   __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopEntry);
****************************
具体要做的事情
****************************
result = __CFRunLoopRun(rl, currentMode, seconds, returnAfterSourceHandled, previousMode);
****************************
通知Observers : 退出RunLoop
****************************
__CFRunLoopDoObservers(rl, currentMode, kCFRunLoopExit);
...
    return result;
}

由于源码过于复杂,就不贴出来了,大家可以自己下载源码去看

RunLoop休眠的实现原理

static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInterval seconds, Boolean stopAfterHandle, CFRunLoopModeRef previousMode) {
  if (NULL != reply) {
            (void)mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0, MACH_PORT_NULL, 0, MACH_PORT_NULL);
            CFAllocatorDeallocate(kCFAllocatorSystemDefault, reply);
        }
}

RunLoop在实际开中的应用

  • 控制线程生命周期(线程保活)
    示例:
@interface NJFThread : NSThread
@end

@implementation NJFThread

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

@end
@interface ViewController ()
@property (strong, nonatomic) NJFThread *thread;
@property (assign, nonatomic, getter=isStoped) BOOL stopped;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    self.stopped = NO;
    self.thread = [[NJFThread alloc] initWithBlock:^{
        NSLog(@"%@----begin----", [NSThread currentThread]);
        ***************************************
         往RunLoop里面添加Source\Timer\Observer
        ***************************************
        [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
        while (weakSelf && !weakSelf.isStoped) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
        NSLog(@"%@----end----", [NSThread currentThread]);
    }];
    [self.thread start];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.thread) return;
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
}

// 子线程需要执行的任务
- (void)test
{
    NSLog(@"%s %@", __func__, [NSThread currentThread]);
}

- (IBAction)stop {
    if (!self.thread) return;
    ***************************************
    在子线程调用stop(waitUntilDone设置为YES,代表子线程的代码执行完毕后,这个方法才会往下走)
    ***************************************
    [self performSelector:@selector(stopThread) onThread:self.thread withObject:nil waitUntilDone:YES];
}

// 用于停止子线程的RunLoop
- (void)stopThread
{
    // 设置标记为YES
    self.stopped = YES;
    // 停止RunLoop
    CFRunLoopStop(CFRunLoopGetCurrent());
    NSLog(@"%s %@", __func__, [NSThread currentThread]);
    // 清空线程
    self.thread = nil;
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
    [self stop];
}

@end

OC封装

typedef void (^NJFPermenantThreadTask)(void);

@interface NJFPermenantThread : NSObject

/**
 开启线程
 */
//- (void)run;

/**
 在当前子线程执行一个任务
 */
- (void)executeTask:(NJFPermenantThreadTask)task;

/**
 结束线程
 */
- (void)stop;

@end
@interface NJFThread : NSThread
@end
@implementation NJFThread
- (void)dealloc
{
    NSLog(@"%s", __func__);
}
@end

/** NJFPermenantThread **/
@interface NJFPermenantThread()
@property (strong, nonatomic) NJFThread *innerThread;
@property (assign, nonatomic, getter=isStopped) BOOL stopped;
@end

@implementation NJFPermenantThread
#pragma mark - public methods
- (instancetype)init
{
    if (self = [super init]) {
        self.stopped = NO;
        
        __weak typeof(self) weakSelf = self;
        
        self.innerThread = [[NJFThread alloc] initWithBlock:^{
             NSLog(@"%@----begin----", [NSThread currentThread]);
            [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
            
            while (weakSelf && !weakSelf.isStopped) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }
            NSLog(@"%@----end----", [NSThread currentThread]);
            
        }];
        
        [self.innerThread start];
    }
    return self;
}

//- (void)run
//{
//    if (!self.innerThread) return;
//
//    [self.innerThread start];
//}

- (void)executeTask:(NJFPermenantThreadTask)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:(NJFPermenantThreadTask)task
{
    task();
}

@end

C的封装

#import "NJFPermenantThread.h"

/** NJFThread **/
@interface NJFThread : NSThread
@end
@implementation NJFThread
- (void)dealloc
{
    NSLog(@"%s", __func__);
}
@end

/** NJFPermenantThread **/
@interface NJFPermenantThread()
@property (strong, nonatomic) NJFThread *innerThread;
@end

@implementation NJFPermenantThread
#pragma mark - public methods
- (instancetype)init
{
    if (self = [super init]) {
        self.innerThread = [[NJFThread alloc] initWithBlock:^{
            NSLog(@"begin----");
            // 创建上下文(要初始化一下结构体)
            CFRunLoopSourceContext context = {0};
            // 创建source
            CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
            // 往Runloop中添加source
            CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
            // 销毁source
            CFRelease(source);
            // 启动
            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, false);
            NSLog(@"end----");
        }];
        
        [self.innerThread start];
    }
    return self;
}

//- (void)run
//{
//    if (!self.innerThread) return;
//
//    [self.innerThread start];
//}

- (void)executeTask:(NJFPermenantThreadTask)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
{
    CFRunLoopStop(CFRunLoopGetCurrent());
    self.innerThread = nil;
}

- (void)__executeTask:(NJFPermenantThreadTask)task
{
    task();
}

@end
  • 解决NSTimer在滑动时停止工作的问题

  • 监控应用卡顿

  • 性能优化

你可能感兴趣的:(RunLoop解析)