如何将[NSRunLoop runUntilDate:]重写为GCD代码?

时间:2012-02-09 04:03:23

标签: iphone ios grand-central-dispatch nsrunloop

仅仅因为我的好奇心,我正在尝试重写以下代码

// show HUD (with animation)
[SVProgressHUD showWithStatus:@"loading..."];

// wait for HUD to safely finish showing its animation
// (loading HUD will be visible for 1 sec)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]

// dismiss HUD
// (dismiss HUD will be visible for 2 sec)
[SVProgressHUD dismissWithSuccess:@"finished!" afterDelay:2];

使用Grand Central Dispatch进入代码。

我尝试使用dispatch_source_t,dispatch_semaphore_t和dispatch_after, 但效果不好:(我需要你的帮助!

请注意,我不想用任何块包装SVProgressHUD的方法!

1 个答案:

答案 0 :(得分:2)

你真的不能。或者,你可以,但这将是一大堆工作,最终只包装NSRunLoop的东西。

在iOS和Mac OS X的上下文中,主事件循环是一个运行循环,模态面板,HUD等等都是围绕这个细节设计的。

您展示的代码是嵌套的运行循环。您正在外循环中的子循环中有效地运行主运行循环,该循环也由主运行循环维护! (如果你要在内部循环中触发的动作方法上设置断点,你会看到我在回溯中的意思。)