V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
run2016
V2EX  ›  问与答

[求问]dispatch_async 的 main queue 中的代码不执行?

  •  
  •   run2016 · 2017-02-17 13:34:11 +08:00 · 2001 次点击
    这是一个创建于 2647 天前的主题,其中的信息可能已经有所发展或是发生改变。

    为了测试方便,经常在 xcode 中新建一个 project 里边放好几个 target(commond line project),就是跑跑验证比较理论的一些东西。

    昨天发现一个问题,代码如下:

    #import <Foundation/Foundation.h>
    @interface Test6 : NSObject
    @end
    
    @implementation Test6
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            NSLog(@"1.current_queue = %@",[NSOperationQueue currentQueue]);
            /*打印:
            current_queue = <NSOperationQueue: 0x1003026c0>{name = 'NSOperationQueue Main Queue'}*/
            
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"这个 block 不会被运行到---");
            });
        }
        return self;
    }
    

    查了网上的一些资料,说是 dispatch_get_main_queue 一般是用来异步执行完任务后回到主线程刷新 UI 来的。我是直接在 main.m 中测试,这个 Test6 测试类是个普通的 NSObject ,文件中也没有导入 UIKit。难道是因为这个原因?但是按理说当前 queue 不应该就是 main_queque?那为什么 block 就是运行不到呢?

    谢谢各位前辈们的不吝赐教。在下菜鸟一只,不甚感激。。

    O(∩_∩)O~

    1 条回复    2017-02-17 14:17:18 +08:00
    flicker317
        1
    flicker317  
       2017-02-17 14:17:18 +08:00   ❤️ 1
    GCD 的 main queue 是跑在 runloop 里的, 你的 main 里没调起 runloop 吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2510 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:51 · PVG 15:51 · LAX 00:51 · JFK 03:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.