Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check an dispatch_async block has finished running

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.

like image 231
jimbob Avatar asked Jul 18 '12 21:07

jimbob


People also ask

Does dispatch_ async create a thread?

When using dispatch_async for a background queue, GCD (Grand Central Dispatch) will ask the kernel for a thread, where the kernel either creates one, picks an idle thread, or waits for one to become idle. These threads, once created, live in the thread pool.

What does dispatch_ async do?

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

What is dispatch_ async in iOS?

Submits a block object for execution and returns after that block finishes executing. iOS 4.0+ iPadOS 4.0+ macOS 10.6+ Mac Catalyst 13.1+ tvOS 9.0+ watchOS 2.0+


1 Answers

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];
    });
});
like image 131
Johnnywho Avatar answered Oct 21 '22 11:10

Johnnywho