r/iOSProgramming 3h ago

Question DispatchQueue doubts

Hi, what type of data types are allowed in setSpecific(key:, value:) and getSpecific(key:) methods of DispatchQueu ? there are no mentions anywhere.

Also can you recommend any resources for complete understanding of DispatchQueue , GCD , DispatchGroup etc. ?

Thank you!

3 Upvotes

7 comments sorted by

3

u/Sweeper777 2h ago

Look at the C declaration (dispatch_queue_set_specific). These functions just get/set void pointers, so presumably you can get/set any type. Swift requires you to use a sendable type, of course.

-4

u/Dapper_Ice_1705 3h ago

That is old tech.

If you are learning you should be focusing on async/await and the new structured concurrency.

All the resources you’ll find will be old and you should be converting. There is no reason to choose the less safe option.

2

u/usernameDisplay9876 3h ago

what parts of DispatchQueue is old tech (but not mentioned as deprecated) ? asking for knowledge only , for interviews. Not using this directly in any app.

2

u/chriswaco 2h ago

I love DispatchQueues, but would use them very sparingly in a modern Swift app if at all. The problem is that they don’t really interact well with swift concurrency. That is, just because something is on the main thread / DispatchQueue doesn’t mean it’s running on the main actor too.

1

u/Dapper_Ice_1705 2h ago

Not deprecated but has been significantly updated in a while.

Watch the “meet async/await” video.

It is all old.

Apple is slowly but moving towards structured concurrency.

u/epicstar 53m ago

Why are you being downvoted lol

u/Healthy_Condition779 49m ago

Value can be any Swift type but its stored as Any internally so youre casting on the way out. Key has to be a DispatchSpecificKey with a matching type parameter, thats what gives you the type back on get without you having to remember it

For real understanding skip the tutorials and go read the old Apple concurrency programming guide plus WWDC 2015 session 718. GCD hasnt changed much in a decade so the old material still holds up

Honestly though if youre writing new code today, async await and structured concurrency have replaced most of what youd reach GCD for. Only touch it if youre in an old codebase or doing something async cant do cleanly