iOS多线程之RunLoop

2017-03-01  本文已影响179人  magic_pill
一、什么是RunLoop
[NSRunLoop currentRunLoop] //获得当前线程的NSRunLoop对象
[NSRunLoop mainRunLoop]    //获得主线程的NSRunLoop对象
CFRunLoopGetCurrent()  //获得当前线程的RunLoop对象
CFRunLoopGetMain() //获得主线程的RunLoop对象

二、RunLoop相关类:

//调用scheduledTimer返回的定时器,已经自动被添加到当前的runloop中,而且模式为NSDefaultRunLoopMode,一旦RunLoop进入其他模式,这个定时器就不会工作。可以通过返回值进行模式修改
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
//以下两行代码和上一句代码效果相同,当发生拖拽行为时,就停止调用run方法
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
//只有在拖拽情况下才调用run方法,定时器只在UITrackingRunLoopMode模式下工作
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
//定时器跑在标记为CommonModes的模式下,CommonModes包含:UITrackingRunLoopMode、kCFRunLoopDefaultMode
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
    kCFRunLoopEntry = (1UL << 0),
    kCFRunLoopBeforeTimers = (1UL << 1),
    kCFRunLoopBeforeSources = (1UL << 2),
    kCFRunLoopBeforeWaiting = (1UL << 5),
    kCFRunLoopAfterWaiting = (1UL << 6),
    kCFRunLoopExit = (1UL << 7),
    kCFRunLoopAllActivities = 0x0FFFFFFFU
};
//创建observer
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
    NSLog(@"observer:监听到RunLoop状态发生改变-----%lu",activity);
});
//添加观察者:监听RunLoop的状态
CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode
//释放observer
CFRelease(observer);

三、RunLoop处理逻辑

四、RunLoop应用

[self.imageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"place"] afterDelay:2.0 inModes:@[NSRunLoopCommonModes]];
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
-(void)viewDidLoad {
        [super viewDidLoad];
        _thread = [[NSThread alloc] initWithTarget:self selector:@selector(excute) object:nil];
        [_thread start];
}
-(void)excute
{
        //首先创建一个timer,然后将timer添加到runloop,再启动runloop
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
}
-(void)viewDidLoad {
        [super viewDidLoad];
        _thread = [[NSThread alloc] initWithTarget:self selector:@selector(excute) object:nil];
        [_thread start];
}
-(void)excute
{
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] run];
}

RunLoop常见问题

上一篇 下一篇

猜你喜欢

热点阅读