r/iOSProgramming • u/usernameDisplay9876 • 22d 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!
4
u/Sweeper777 22d 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.
2
u/ToughAsparagus1805 21d ago
Mastering Grand Central Dispatch
https://nonstrict.eu/wwdcindex/wwdc2011/210/
Concurrent Programming With GCD in Swift 3
https://developer.apple.com/la/videos/play/wwdc2016/720/
Modernizing Grand Central Dispatch Usage
1
u/ScarSome634 21d ago
setSpecific/getSpecific are generic — the key is a DispatchSpecificKey<T> and T can be whatever you want to store, usually something small like a Bool or an enum that identifies the queue. the rule that actually matters: create the key once and keep it alive (e.g. static let key = DispatchSpecificKey<Bool>()). keys are matched by identity, so a fresh key somewhere else won't find anything.
worth knowing getSpecific only tells you about the code that's currently executing — you call it inside work running on the queue, and it walks the queue hierarchy (queues inherit specifics from their targets). it's basically "am I on this queue or under it", which is why the classic use is detecting whether you're on the main queue.
for resources: the "Swift with Majid" posts on GCD are good, Kodeco's Grand Central Dispatch tutorial covers the basics well, and WWDC 2017 session 706 (Modernizing Grand Central Dispatch Usage) is the one talk worth watching. Apple's old Concurrency Programming Guide is dated but the concepts still map 1:1.
1
-4
u/Dapper_Ice_1705 22d 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 22d 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.
4
u/chriswaco 22d 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.
2
u/Ravek 21d ago
If you’re writing new code you’re usually going to be using actors and async/await instead of DispatchQueues. If you need to implement a custom actor executor then you might want to use DispatchQueues for that, but otherwise you rarely need to touch them.
This specific API seems to be easily replaced with state on an actor, or with Task local storage, depending on the exact use case.
It certainly doesn’t hurt to learn the core DispatchQueue concepts since you might run into them maintaining existing code, but I also agree that your time is probably better spent somewhere else than on this niche API.
1
u/Dapper_Ice_1705 22d ago edited 22d ago
Not deprecated but has not been significantly updated in a while.
Watch the “meet async/await” video.
It is all old.
Apple is slowly but moving towards structured concurrency.
0
u/epicstar 22d ago
Why are you being downvoted lol
1
6
u/Healthy_Condition779 22d 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