3

I know that GCD's dispatch_get_current_queue has been deprecated, and it is an anti-pattern to use it for callbacks anyway, but is -[NSOperationQueue currentQueue] reliable to use, or does it suffer from the same problems that GCD's dispatch_get_current_queue has? (I just want it so I can assert that I'm on the proper queue when my operation executes.)

Community
  • 1
  • 1
Heath Borders
  • 30,998
  • 16
  • 147
  • 256

1 Answers1

3

dispatch_get_current_queue was not deprecated because it's unreliable. It's deprecated because nearly all uses of it other than assertions are a bad idea, and because there can be more than one simultaneous current queue.

NSOperationQueue does not support target queues or synchronous execution, which are the two reasons the latter issue exists in dispatch, so it should be fine to use for assertions.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • But if multiple queues can be the current queue, why is it `dispatch_get_current_queue` safe to use in an assertion? If you're on two queues, wouldn't it be possible for the wrong one to be returned? Or does `dispatch_get_current_queue` always return the queue to which the current block was most recently dispatched? – Heath Borders Mar 10 '14 at 18:23
  • 1
    dispatch_get_current_queue *isn't* entirely safe to use for an assertion. -[NSOperationQueue currentQueue] doesn't have that issue because NSOperationQueue doesn't have some features that dispatch queues have. – Catfish_Man Mar 10 '14 at 18:42
  • Ok, so when you say "All uses of [`dispatch_get_current_queue`] other than assertions are a bad idea", you mean that you need to know that the GCD queue is never used simultaneously with another queue in order to use it in an assertion? – Heath Borders Mar 10 '14 at 18:45
  • 1
    Yes, exactly. It's tricky to use in assertions, but for a limited subset of cases it can be done. – Catfish_Man Mar 10 '14 at 18:48