Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Wait for synchronous tasks:</h3> <p>I'd recommend creating a new <code>NSOperationQueue</code>, configure it to have as many threads as you need and then you launch your operations on that thread. To wait for them to complete, simply call <code>waitUntilAllOperationsAreFinished</code> method.</p> <p>For example:</p> <pre><code>NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init]; // Add your tasks here [operationQueue addOperationWithBlock:^{ // Long running task }]; [operationQueue waitUntilAllOperationsAreFinished]; </code></pre> <p>Be careful to not block the main thread or your Application will become unresponsive while it loads.</p> <p>If you need something more complex than this, you can use the lower level API <code>dispatch_group_t</code> to group threads and wait for them. Take a look at <a href="https://stackoverflow.com/a/10644282/469218">this response</a> for a deeper explanation.</p> <h3>Wait for already asynchronous tasks:</h3> <p>Sometimes the methods described above are not enough, probably because you need to wait for an already asynchronous task (like enabling GPS or waiting for some external event). In these cases you can use a <strong>semaphore</strong>. You have to be very careful with error handling and semaphores, or you could block a thread forever.</p> <pre><code>dispatch_semaphore_t sema = dispatch_semaphore_create(0); [object performLongOperationAndDo:^{ dispatch_semaphore_signal(sema); }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_release(sema); </code></pre> <p>More <a href="http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html" rel="nofollow noreferrer">info about Obj-C semaphores</a>.</p> <p>Unless necessary, I'd stick to <code>NSOperationQueue</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload