how to check an dispatch_async block has finished running

夙愿已清 提交于 2020-01-01 05:13:07

问题


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.

I have a queue which I have called json_queue.

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

I then have a dispatch_async block with this syntax:

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

It wont let me perform the line: "[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...

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

Thankyou lovely people.

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


回答1:


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

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



回答2:


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

        });
    });


来源:https://stackoverflow.com/questions/11550962/how-to-check-an-dispatch-async-block-has-finished-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!