为什么 Dispatch_group_notify 在不同的环境下工作不同?
Why Dispatch_group_notify works different in different environment?
第一种情况是我创建了一个命令行工具应用程序,运行这段代码。
NSLog(@"Main:%@", [NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"Task1:%@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"Task2:%@", [NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
登录终端是
Main:<NSThread: 0x1028033b0>{number = 1, name = main}
Task2:<NSThread: 0x10040f0f0>{number = 2, name = (null)}
Task1:<NSThread: 0x1006008d0>{number = 3, name = (null)}
如果我想显示上次登录 queue
并替换 main queue
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
和queue
dispatch_group_notify(group, queue, ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
终端打印最后一个log.But为什么它不能在主队列中撤销?
当我将此代码复制到简单 iOS Application.All 时效果很好:
Main:<NSThread: 0x600000070ac0>{number = 1, name = main}
Task2:<NSThread: 0x6000002633c0>{number = 3, name = (null)}
Task1:<NSThread: 0x600000263480>{number = 4, name = (null)}
MainFinish:<NSThread: 0x600000070ac0>{number = 1, name = main}
我尝试在 'Command Tool Line' 中的 Task1 上添加 sleep(1)
,但它似乎阻塞了队列并且只打印 log:Task2...但这一切在简单 iOS申请.
为什么会导致这些不同?
image here
与其他在创建时激活的队列不同,您应该调用 dispatch_main()
方法来执行提交给主线程的块
image here
相同的代码在iOS应用程序中运行良好的原因是应用程序启动了一个默认的runloop,它根据一定的规则执行提交到主队列的任务,例如UI更新通知.
参考如下:
第一种情况是我创建了一个命令行工具应用程序,运行这段代码。
NSLog(@"Main:%@", [NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"Task1:%@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"Task2:%@", [NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
登录终端是
Main:<NSThread: 0x1028033b0>{number = 1, name = main}
Task2:<NSThread: 0x10040f0f0>{number = 2, name = (null)}
Task1:<NSThread: 0x1006008d0>{number = 3, name = (null)}
如果我想显示上次登录 queue
并替换 main queue
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
和queue
dispatch_group_notify(group, queue, ^{
NSLog(@"Finish:%@", [NSThread currentThread]);
});
终端打印最后一个log.But为什么它不能在主队列中撤销?
当我将此代码复制到简单 iOS Application.All 时效果很好:
Main:<NSThread: 0x600000070ac0>{number = 1, name = main}
Task2:<NSThread: 0x6000002633c0>{number = 3, name = (null)}
Task1:<NSThread: 0x600000263480>{number = 4, name = (null)}
MainFinish:<NSThread: 0x600000070ac0>{number = 1, name = main}
我尝试在 'Command Tool Line' 中的 Task1 上添加 sleep(1)
,但它似乎阻塞了队列并且只打印 log:Task2...但这一切在简单 iOS申请.
为什么会导致这些不同?
image here
与其他在创建时激活的队列不同,您应该调用 dispatch_main()
方法来执行提交给主线程的块
image here
相同的代码在iOS应用程序中运行良好的原因是应用程序启动了一个默认的runloop,它根据一定的规则执行提交到主队列的任务,例如UI更新通知.
参考如下: