Group Group Group Group Group Group Group Group Group

Chapter 4 : Semaphores Last example what is use of defined queue

I have a question regarding the last example.

let group = DispatchGroup()
let queue = .userInitiated)

let base = ""
let ids = [ 466881, 466910, 466925, 466931, 466978, 467028, 467032, 467042, 467052 ]
var images: [UIImage] = []

for id in ids {
    guard let url = URL(string: "\(base)\(id)-jpeg.jpg") else { continue }
    let task = URLSession.shared.dataTask(with: url) { data, _,error in
        defer { group.leave()}
        if error == nil, let data = data, let image = UIImage(data: data) {

group.notify(queue: queue) {
    let result = images

What is the use of queue here? I don’t see it being used.
URLSession is running in the background queue. the only reason I see it here as notify need the queue in the signature. Otherwise no real use. I am not running anything on it to be notified.

Thanks for reading

You have to tell group.notify(queue:) which queue to send the notification on. The let queue line is there mostly just to make it clear that it’s a dispatch queue. You’d pass in whatever queue you’re working with. In this case, you might even send it to the main queue as you probably are ready to update the UI at that point.