What is the best/simplest way to detect if a queue I have created is currently in use? My intent here is to defer a particular action until all queued tasks have completed.
Thanks,
Doug
UPDATE
Here is a bit more context. I have an async serial queue (private dispatch queue) that is a property of my object. When this queue completes its work it calls dispatch_async(dispatch_get_main_queue(), {...}) to up date the UI. Currently the app runs only in landscape interface orientation. I am now adding support for both portrait & landscape interface orientation. For now, I prefer not to allow the interface to rotate to/from portrait/landscape while my GCD async task is running. So in my controller’s shouldAutorotateToInterfaceOrientation: method I want to return NO during those periods.
There’s no direct support for that in GCD. You can probably get the effect you’re looking for in other ways, though.
Do you just want to run a block after other blocks have completed? Put the blocks in a
dispatch_group, and submit your “after everything’s done action” usingdispatch_group_notify. This works with concurrent or sequential queues.Similarly, put your blocks in a group, and call
dispatch_group_wait(group, DISPATCH_TIME_NOW). If it returns non-zero, then there are outstanding blocks in the group.Is your queue sequential, and you want to wait until the other blocks have completed, blocking the current thread until then? Just issue your handler using
dispatch_syncon the same queue.There are other possibilities, depending on your exact situation.
Edit: Based on your update, there’s an even easier way:
BOOLproperty to your view controller, calledallowsOrientationChange-shouldAutorotateToInterfaceOrientation:to returnself.allowsOrientationChangedispatch_asyncto set the property to NOdispatch_asyncto set the property to YES(There’s a little bit of a race condition between when the property is set to NO on the main thread, and when the rest of your work happens in the queue. You could make it a
dispatch_syncif that really matters. Depends on what your reasons are for preventing rotation, I suppose.)