美文网首页iOS开发
block在主线程还是在分线程中执行

block在主线程还是在分线程中执行

作者: xing_xing | 来源:发表于2017-04-13 15:04 被阅读248次

    答案: 在哪条线程调用就在哪条线程中执行

    @property (nonatomic, copy) void(^block)(NSString *name);
    
    self.block = ^(NSString *name) {
        NSLog(@"%@%@",name,[NSThread currentThread]);
    };
    
    self.block(@"1");
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"当前在%@",[NSThread currentThread]);
        self.block(@"2");
    });
    

    输入 :
    2017-04-13 14:42:00.954 LZX[1562:106772] 1<NSThread: 0x608000062040>{number = 1, name = main}
    2017-04-13 14:42:00.954 LZX[1562:106811] 当前在<NSThread: 0x60800006dac0>{number = 3, name = (null)}
    2017-04-13 14:42:00.955 LZX[1562:106811] 2<NSThread: 0x60800006dac0>{number = 3, name = (null)}

    相关文章

      网友评论

        本文标题:block在主线程还是在分线程中执行

        本文链接:https://www.haomeiwen.com/subject/fumbattx.html