RunLoop在实际开发过程中的应用(二)

96
王鑫20111
0.6 2017.04.19 16:03* 字数 620

Tip

  • 1.UIImageView延迟加载照片
  • 2.线程保活
  • 3.子线程中执行NSTimer
  • 4.performSelector
  • 5.自动释放池
一.UIImageView延迟加载照片

在实际的开发过程中和面试题中,我们总去说可以在tableview的滑动的时候不去加载照片,因为渲染可能会阻塞主线程,我们总说:可以再tableview停止滑动的时候再去渲染照片,现在可以好好聊聊这个话题

//控制中写上这个方法即可,3秒钟然后显示照片
//这个方法,我看就可以在滑动的时候,延迟显示照片
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.imageview performSelector:@selector(setImage:)
                         withObject:[UIImage imageNamed:@"123.png"]
                         afterDelay:3];
}

//这个是第二种清空,然后直接指定什么模式,二者等价
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.imageview performSelector:@selector(setImage:)
                         withObject:[UIImage imageNamed:@"123.png"]
                         afterDelay:3
     inModes:@[NSDefaultRunLoopMode]];
}

滑动的时候,阻止了照片显示,因为它默认是在Default下显示, 此时tableView在滑动,RunLoop属于Tracking的模式, 所以3秒钟显示的目的被延时了
  • 如果想让照片的显示或者timer的运行在任何时候都好使怎么办?
    设置为commonMode就行了
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.imageview performSelector:@selector(setImage:)
                         withObject:[UIImage imageNamed:@"123.png"]
                         afterDelay:3
     inModes:@[NSRunLoopCommonModes]];
}
二.线程保活

可能你的项目中需要一个线程,一直在后台做些耗时操作,但是不影响主线程,我们不要一直大量的创建和销毁线程,因为这样太浪费性能了,我们只要保留这个线程,只要对他进行“保活”就行

//继承了一个NSTread 线程,然后使用vc中创建和执行某个任务,查看线程的情况
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    WXThread *thread = [[WXThread alloc] initWithTarget:self
                                               selector:@selector(doSomeThing)
                                                 object:nil];
    [thread start];
}
- (void)doSomeThing{
    NSLog(@"doSomeThing");
}
//每一次点击屏幕的时候,线程执行完方法,直接释放掉了,下一次创建了一个新的线程;
//子线程存活的时间很短,只要执行完毕任务,就会被释放
2017-04-19 16:03:10.686 WXAllTest[14928:325108] doSomeThing
2017-04-19 16:03:10.688 WXAllTest[14928:325108] WXTread - dealloc - <WXThread: 0x600000276780>{number = 3, name = (null)}
2017-04-19 16:03:18.247 WXAllTest[14928:325194] doSomeThing
2017-04-19 16:03:18.249 WXAllTest[14928:325194] WXTread - dealloc - <WXThread: 0x608000271340>{number = 4, name = (null)}
2017-04-19 16:03:23.780 WXAllTest[14928:325236] doSomeThing
2017-04-19 16:03:23.781 WXAllTest[14928:325236] WXTread - dealloc - <WXThread: 0x608000270e00>{number = 5, name = (null)}

如果我每隔一段时间就像在线程中执行某个操作,好像现在不行
如果我们将线程对象强引用,也是不行的,会崩溃

1.成为基本属性
/** 线程对象 */
@property(strong,nonatomic)  WXThread *thread;

2.创建线程之后,直接将入到RunLoop中
- (void)viewDidLoad {
    [super viewDidLoad];
    _thread = [[WXThread alloc] initWithTarget:self
                                      selector:@selector(doSomeThing)
                                        object:nil];
    [_thread start];
}

3.执行doSomeThing函数
- (void)doSomeThing{
    //一定要加入一个timer,port,或者是obervers,否则RunLoop启动不起来
    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}

4.在点击屏幕的时候,执行一个方法,线程之间的数据通信

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:NO modes:@[NSDefaultRunLoopMode]];
}

5.将test方法写清楚
- (void)test{
    NSLog(@"current thread - %@",[NSThread currentThread]);
}


//打印结果:同一个线程,线程保活成功
2017-04-19 18:21:07.660 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:07.843 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:08.015 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:08.194 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:08.398 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:08.598 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
2017-04-19 18:21:08.770 WXAllTest[16145:382366] current thread - <WXThread: 0x60800007c180>{number = 3, name = (null)}
三.子线程中执行NSTimer

刚才学习了在RunLoop中去处理源,source (seletor),现在来看看如何在子线程中处理NSTimer

/** 线程对象 */
@property(strong,nonatomic)  WXThread *thread;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //延迟图片加载
    
    _thread = [[WXThread alloc] initWithTarget:self
                                      selector:@selector(execute)
                                        object:nil];
    [_thread start];
}

- (void)execute{
    //该方法默认不加入RunLoop中,使用schedule可以
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.3
                                             target:self
                                           selector:@selector(test2)
                                           userInfo:nil
                                            repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}

- (void)test2{
    NSLog(@"current thread ********** 2 - %@",[NSThread currentThread]);
}

//打印:
2017-04-19 18:45:18.342 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:18.643 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:18.943 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:19.243 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:19.544 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:19.842 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:20.143 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:20.443 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:20.743 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:21.042 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}
2017-04-19 18:45:21.342 WXAllTest[16508:398383] current thread ********** 2 - <WXThread: 0x608000261380>{number = 3, name = (null)}

注意: 这两者相同,后者默认直接创建了RunLoop,然后加进去了,但是一定要run才能启动,但是过去我们没有在主线程中run,也好使啊,为毛?因为在主线程中系统自动run了,否则线程早就停止了

    NSTimer *timer = [NSTimer timerWithTimeInterval:0.3
                                             target:self
                                           selector:@selector(test2)
                                           userInfo:nil
                                            repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
    [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(test2)
                                                    userInfo:nil
                                                     repeats:YES];
    [[NSRunLoop currentRunLoop] run];

疑惑:滑动textview的时候,模式已经改成了Tracking,但是我们的timer是default模式,为什么没有影响?

因为textView是主线程的,而timer是子线程的东西,所以没关系

五.自动释放池

1.什么是自动释放池,就是将目前使用到的对象放到池子中,然后当自动释放池销毁的时候,对内部的所有对象都进行realese操作,retrainCount减一
2.自动释放池什么时候销毁和创建?
当每一次要进入睡眠状态,那么就会销毁,当即将醒来的时候,重新创建

一个RunLoop对应一个线程
建议每一次启动RunLoop的时候,包装一个自动释放池,临时创建了很多对象,等着我们释放,在很多优秀的开源库中,都有这个说明

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //延迟图片加载
    
    _thread = [[WXThread alloc] initWithTarget:self
                                      selector:@selector(execute)
                                        object:nil];
    [_thread start];
}

- (void)execute{
//    该方法默认不加入RunLoop中,使用schedule可以
    @autoreleasepool {
        NSTimer *timer = [NSTimer timerWithTimeInterval:0.3
                                                 target:self
                                               selector:@selector(test2)
                                               userInfo:nil
                                                repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    }
}
日记本