• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Ordering of Completion Handlers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ordering of Completion Handlers


  • Subject: Re: Ordering of Completion Handlers
  • From: Seth Willits <email@hidden>
  • Date: Fri, 31 Aug 2012 10:35:14 -0700

On Aug 31, 2012, at 1:08 AM, Andreas Grosam wrote:

> I have a number of operations that will be received serially. Each operation can be processed concurrently with respect to each other. I would like to invoke the completion blocks for each operation in the order as they have been received.



If you have operations A, B, C, D where which should execute concurrently, but their results used serially then:


dispatch_queue_t serialQueue = …;
dispatch_queue_t concurrentQueue = (global queue, or a new concurrent queue);




dispatch_semaphore_t semaphoreA = dispatch_semaphore_create(0);
__block id resultsOfA = nil;


wrapA = ^{
	resultsOfA = runA();
	dispatch_semaphore_signal(semaphoreA);
}


doneA =^{
	dispatch_semaphore_wait(semaphoreA);
	completionHandlerForA(resultsOfA);
}




// the completion handlers are pushed onto a serial queue
// but will not execute until the semaphore is signaled
// after the work is done
dipatch_async(doneA, searialQueue);
dipatch_async(doneB, searialQueue);
dipatch_async(doneC, searialQueue);
dipatch_async(doneD, searialQueue);

// The work fires off concurrently
dispatch_async(wrapA, concurrentQueue);
dispatch_async(wrapB, concurrentQueue);
dispatch_async(wrapC, concurrentQueue);
dispatch_async(wrapD, concurrentQueue);




> I'm searching a simple approach to achieve this, implemented using NSOperationQueue, NSOperation and dispatch lib.


You can do the above with NSOperationQueue using a serial queue and concurrent queue, but you must use dependencies between the operations to enforce the serial execution of the completion handlers. The standard behavior of a serial NSOpQ is that only one thing may be executed at a time, but it *does not care which order they were submitted in*. It's first *ready* first out, not first in first out. So although you may have a dependency that the completion handler operation come after the work operation, if B finishes before A, B's completion handler will run first. To stop that you need to have a dependency that A complete before B. The trouble with this is that it ends up with a chain of retains through the dependencies properties (dependencies aren't dropped from the operation even if they're met), so you need to manually remove it from within the operation itself. It's messy.




--
Seth Willits




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >Ordering of Completion Handlers (From: Andreas Grosam <email@hidden>)

  • Prev by Date: Re: Network and DarkWake
  • Next by Date: Re: How to insert a "screen-only" character in an NSTextView?
  • Previous by thread: Ordering of Completion Handlers
  • Next by thread: Network and DarkWake
  • Index(es):
    • Date
    • Thread