理解的主线程刷新UI:

来源:互联网 发布:程序员团队建设 编辑:程序博客网 时间:2024/05/26 09:56

RunLoop传送门

线程安全传送门

结论:

1.一切有关UI的操作,涉及操作程序用户界面的类,都需要在主线程执行,因为UIKit(包括绘图)非线程安全,如果两个子线程同时操作同一个图片,将会导致APP崩溃,而且释放也是会成为一个问题,同一个视图对象只能被释放一次,所以要在主线程中执行;

2.在操作UI时,比如改变了 Frame、更新了 UIView/CALayer 的层次时,或者手动调用了 UIView/CALayer 的 setNeedsLayout/setNeedsDisplay方法后,这个 UIView/CALayer 会先被标记为待处理,并被提交到一个全局的容器去。之后通过Observer监听事件调用函数再遍历所有待处理的 UIView/CAlayer 以执行实际的绘制和调整,并更新 UI 界面。所以,子线程会导致出现延时提交或者无法提交,而放在主线程就不会有如此影响用户交互的情况出现。


摘自RunLoop界面更新说明:

当在操作 UI 时,比如改变了 Frame、更新了 UIView/CALayer 的层次时,或者手动调用了 UIView/CALayer 的 setNeedsLayout/setNeedsDisplay方法后,这个 UIView/CALayer 就被标记为待处理,并被提交到一个全局的容器去。

苹果注册了一个 Observer 监听 BeforeWaiting(即将进入休眠) 和 Exit (即将退出Loop) 事件,回调去执行一个很长的函数:

_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv()。这个函数里会遍历所有待处理的 UIView/CAlayer 以执行实际的绘制和调整,并更新 UI 界面。


查:此例并不属实,目前的Xcode并没有达到楼主的输出结果,现在的结果刷新延迟10秒,所以抛弃。

http://blog.sina.com.cn/s/blog_45e2b66c0102v254.html

验码,并不能出现子线程立即刷新自己的button

- (void)viewDidLoad {

    [superviewDidLoad];


    _view1=[[UIViewalloc]initWithFrame:CGRectMake(0,0,200,200)];

    

    UIView *view2=[[UIViewalloc]initWithFrame:CGRectMake(0,300,200,200)];

    

    

    _view1.backgroundColor=[UIColorredColor];

    

    view2.backgroundColor=[UIColorgreenColor];

    

    [self.viewaddSubview:_view1];

    

    [self.viewaddSubview:view2];


    _button=[UIButtonbuttonWithType:UIButtonTypeCustom];

    

   _button.frame=CGRectMake(20,220,50, 50);

    

    _button.backgroundColor=[UIColorblueColor];

    

    [self.viewaddSubview:_button];

    

    

    [_buttonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

}


-(void)buttonAction:(UIButton *)button{

    

   NSLog(@"按下");

    

    [NSThreaddetachNewThreadSelector:@selector(newT)toTarget:selfwithObject:nil];

    

       BOOL main=[NSThreadisMainThread];

    

    NSLog(@"1.......%ld",main);

//

//    button.backgroundColor=[UIColor purpleColor];

}


-(void)newT{

    

       BOOL main=[NSThreadisMainThread];

    

        NSLog(@"2..........%ld",main);


//    sleep(10);

        _button.backgroundColor=[UIColorpurpleColor];

    _view1.backgroundColor=[UIColoryellowColor];

//     sleep(10);

//    [NSThread currentThread]

    [NSThreadsleepForTimeInterval:10];  

}

2016-03-07 14:42:12.979子线程刷新TEST[1040:35640]按下

2016-03-07 14:42:12.980子线程刷新TEST[1040:35640] 1.......1

2016-03-07 14:42:12.980子线程刷新TEST[1040:35683] 2..........0



查:此例并不属实,最下方验码:

http://blog.sina.com.cn/s/blog_45e2b66c0102v254.html

0 0
原创粉丝点击