如何检查dispatch_async块是否已完成运行

如何检查dispatch_async块是否已完成运行

问题描述:

因此基本上我需要能够在块运行完成后运行一个segue。我有一个块做了一些JSON的东西,我需要知道什么时候已经完成运行。如何检查dispatch_async块是否已完成运行

我有一个我称之为json_queue的队列。

jsonQueue = dispatch_queue_create("com.jaboston.jsonQueue", NULL); 

然后我有这个语法dispatch_async块:

dispatch_async(jsonQueue, ^{ 
    [self doSomeJSON]; 
    [self performSegueWithIdentifier:@"modaltomenu" sender:self]; 
    }); 

它不会让我表演的台词: “[自我performSegueWithIdentifier:@” modaltomenu “发件人:自我]”

试图从主线程或Web线程以外的线程获取Web锁。这可能是从辅助线程调用UIKit的结果。现在崩溃...

我在哪里可以检查发现线程什么时候完成了它的肮脏工作,所以我可以调用segue?

谢谢可爱的人。

PS:啤酒和UPS和泰迪熊和鲜花,以谁可以帮助< 3.

你应该叫上只有主线程UI的方法。尝试派遣performSegueWithIdentifier:主队列:

dispatch_async(jsonQueue, ^{ 
    [self doSomeJSON]; 
    dispatch_sync(dispatch_get_main_queue(), ^{ 
     [self performSegueWithIdentifier:@"modaltomenu" sender:self]; 
    }); 
}); 
+0

你先生有upvote和啤酒。 (这是我正在寻找的答案!)。我完全忘了你可以随时跳回到mainqueue。辉煌。 – jimbob 2012-07-18 22:13:20

+0

我是新来的这个东西。为什么这比在块内使用performSelectorOnMainThread更好? – 2012-07-18 22:17:20

+0

@jimbob我应该提到,在第三行使用'dispatch_sync'而不是'dispatch_async'在这种情况下会更好(我已经编辑过)。在WWDC2010 Session 206视频中推荐使用此模式。 @ranReloaded使用另一个块更方便,因为您不需要为这样简单的任务创建全新的方法,而且什么是好的,您可以访问块定义范围内的每个变量。 – Johnnywho 2012-07-18 22:38:35

dispatch_async(jsonQueue, ^{ 
     [self doSomeJSON]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self performSegueWithIdentifier:@"modaltomenu" sender:self]; 
      //Finished with async block 

     }); 
    }); 
+0

我投了它,因为它也是正确的答案,但不幸的是,上面的人回答了一些额外的信息。但是非常感谢! – jimbob 2012-07-18 22:14:48