zoukankan      html  css  js  c++  java
  • 子线程里调用performSelector需要注意什么

    以下代码执行顺序是什么 ?

    - (void)action {

        NSLog(@"1");

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_async(queue, ^{

            NSLog(@"3");

            [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];

            NSLog(@"4");

        });

        NSLog(@"2");

    }

     

    - (void)test {

        NSLog(@"5");

    }

    结果

    2019-07-02 09:24:18.492671+0800 ModelTest[12945:510979] 1

    2019-07-02 09:24:18.492776+0800 ModelTest[12945:510979] 2

    2019-07-02 09:24:18.492807+0800 ModelTest[12945:511386] 3

    2019-07-02 09:24:18.493009+0800 ModelTest[12945:511386] 4

    test方法没有调用。因为子线程里runloop默认是关闭的。改为一下代码后

    - (void)action {

        NSLog(@"1");

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_async(queue, ^{

            NSLog(@"3");

            [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];

            [[NSRunLoop currentRunLoop]run];

            NSLog(@"4");

        });

        NSLog(@"2");

    }

     

    - (void)test {

        NSLog(@"5");

    }

    结果

    2019-07-02 09:38:40.874213+0800 ModelTest[13094:524707] 1

    2019-07-02 09:38:40.874530+0800 ModelTest[13094:524707] 2

    2019-07-02 09:38:40.874551+0800 ModelTest[13094:524802] 3

    2019-07-02 09:38:43.792863+0800 ModelTest[13094:524802] 5

    2019-07-02 09:38:43.793109+0800 ModelTest[13094:524802] 4

  • 相关阅读:
    mysql复制那点事
    全排列问题
    56. Merge Interval
    2. Add Two Numbers
    20. Valid Parentheses
    121. Best Time to Buy and Sell Stock
    120. Triangle
    96. Unique Binary Search Trees
    91. Decode Ways
    72. Edit Distance
  • 原文地址:https://www.cnblogs.com/huangzs/p/11118584.html
Copyright © 2011-2022 走看看