Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

325
Vistas
DispatchGroup notify block being called early

Having an issue with DispatchGroup's notify block being called early in my app and made this playground example to experiment. Based on the output, at times it's being called before even the first .leave(). Feels like I'm missing something obvious and now I've looked at it too long.

let s = DispatchSemaphore(value: 1)
let dg = DispatchGroup()
func go() -> Void {
    for i in 1...2 {
        doWork(attemptNo: i, who: "Lily", secs: Double.random(in: 1.0...5.0))
        doWork(attemptNo: i, who: "Emmie", secs: Double.random(in: 1.0...10.0))
        doWork(attemptNo: i, who: "Wiley", secs: Double.random(in: 1.0...3.0))
    }
}
func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    DispatchQueue.global().async {
        dg.enter()
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")

        DispatchQueue.global().asyncAfter(deadline: .now() + TimeInterval(workSecs)) {
            print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
            s.signal()
            dg.leave()
       }
   }
}
go()
dg.notify(queue: .global(), execute: {print("Everyone is done.")})

Sample output:

Emmie wants to work, will wait 4.674405654828654 seconds, attempt #1
Lily wants to work, will wait 1.5898288206500877 seconds, attempt #1
Wiley wants to work, will wait 1.2182416407288 seconds, attempt #1
Lily wants to work, will wait 3.3225083978280647 seconds, attempt #2
Everyone is done.
Wiley wants to work, will wait 2.801577828588925 seconds, attempt #2
Emmie wants to work, will wait 8.9696422949966 seconds, attempt #2
Lily went to work for 3 seconds on task #2
Wiley was denied. No soup for me! Task #1 not going to happen.
Lily was denied. No soup for me! Task #1 not going to happen.
Wiley was denied. No soup for me! Task #2 not going to happen.
Lily is sliding down the dinosaur tail. Task #2 all done!
Emmie went to work for 3 seconds on task #1
Emmie is sliding down the dinosaur tail. Task #1 all done!
Emmie went to work for 2 seconds on task #2
Emmie is sliding down the dinosaur tail. Task #2 all done!

In this case, "everyone is done" happens almost immediately and since the .leave are along with either not getting the semaphore or after the "work" is done, this doesn't make sense. Help please.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Your primary issue is that you call dg.enter() in the wrong place. You always want to call enter() before the async call and you want to call leave() when the async call is finished.

As your code is written now, the for loop finishes before the first call to enter is ever made which is why notify is triggered immediately.

func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    dg.enter()
    DispatchQueue.global().async {
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
        sleep(workSecs)
        print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
        s.signal()
        dg.leave()
    }
}

Your code also makes weird use of a semaphore and sleep. I guess this an attempt to simulate a long running background process.

over 4 years ago · Santiago Trujillo Denunciar

0

That is simple, doWork returns before entering the group ...

this should work, as expected

import Foundation

let s = DispatchSemaphore(value: 1)
let dg = DispatchGroup()
func go() -> Void {
    for i in 1...2 {
        doWork(attemptNo: i, who: "Lily", secs: Double.random(in: 1.0...5.0))
        doWork(attemptNo: i, who: "Emmie", secs: Double.random(in: 1.0...10.0))
        doWork(attemptNo: i, who: "Wiley", secs: Double.random(in: 1.0...3.0))
    }
}
func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    dg.enter()
    DispatchQueue.global().async {
        //dg.enter()
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
        sleep(workSecs)
        print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
        s.signal()
        dg.leave()
    }
}
go()
dg.notify(queue: .global(), execute: {print("Everyone is done.")})

The best way to avoid such a mistake is to use proper API

    func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
        DispatchQueue.global().async(group: dg) {
            print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
            if s.wait(timeout: .now() + secs) == .timedOut {
                print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
                return
            }
            let workSecs = UInt32(Int.random(in: 1...3))
            print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
            sleep(workSecs)
            print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
            s.signal()
        }
    }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda