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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11550962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:56:08  来源:igfitidea点击:

how to check an dispatch_async block has finished running

iphoneiosxcodegrand-central-dispatch

提问by jimbob

So basically I need to be able to run a segue after a block has finished running. I have a block which does some JSON stuff and I need to know when that has finished running.

所以基本上我需要能够在块完成运行后运行 segue。我有一个执行一些 JSON 内容的块,我需要知道它何时完成运行。

I have a queue which I have called json_queue.

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

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

I then have a dispatch_async block with this syntax:

然后我有一个具有以下语法的 dispatch_async 块:

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

It wont let me perform the line: "[self performSegueWithIdentifier:@"modaltomenu" sender:self];"

它不会让我执行以下行:“[self performSegueWithIdentifier:@"modaltomenu" sender:self];”

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

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

Where can I check to find out when the thread has done its dirty work so i can call the segue?

我在哪里可以检查以找出线程何时完成其肮脏的工作,以便我可以调用 segue?

Thankyou lovely people.

谢谢可爱的人。

PS: beer and ups and teddy bears and flowers to whoever can help <3.

PS:啤酒和起居室以及泰迪熊和鲜花给任何可以帮助的人<3。

回答by Johnnywho

You should call UI methods on main thread only. Try to dispatch performSegueWithIdentifier: on main queue:

您应该只在主线程上调用 UI 方法。尝试在主队列上调度 performSegueWithIdentifier:

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

回答by shabbirv

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

        });
    });