Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As mentioned in the comments the simplest solution is to run the callback only when the final task has completed. You can then call the callback code for all the tasks sequentially. I.e. add the objects into an array if needed and then when a incremental counter in the callback reaches the number of objects in the array, you perform the callback code.</p> <p>If the callback is object independent, then you can simply use counters and run code after completion. Remember to run any counter manipulation also in the main thread or with a '@sync()' directive to avoid race conditions.</p> <pre><code>dispatch_async(dispatch_get_main_queue(), ^(void){ /* code*/ }); </code></pre> <p>Edit: using the same array technique in the completion handler you set flag of the object as ready to be sent. Then traverse the array as far as possible and send all the objects that are ready sequentially. Otherwise stop and wait for the next completion handler call. You can use a counter to keep track of position or remove items from the array as they are done, but be sure to do it on the main thread or with a sync block.</p> <pre><code>@interface MyImage : NSImage @property (assign) BOOL ready; @end @implementation MyImage @synthesize ready; - (void)send { //Send image, make sure it's threaded send, NSConnection should be okay } @end NSArray *imagesNeededToSend = [NSMutableArray arrayWithObjects:image1, image2, image3, nil]; dispatch_async(self.queue, ^{ // Long-running code here of varying complexity dispatch_async(dispatch_get_main_queue(), ^{ self.ready = YES; dispatch_async(dispatch_get_main_queue(), ^(void){ [self sendImages] }); }); }); ... - (void)sendImages { MyImage *firstImage = [imagesNeededToSend firstObject]; //Xcode Beta, but you should have a a category for firstObject, very useful. if (firstImage.ready) { [firstImage send]; [imagesNeededToSend removeObjectAtIndex:0]; [self sendImages]; } } </code></pre>
 

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