Search code examples
iosswiftgrand-central-dispatchdispatchgroup

Swift -is it necessary to call continue when leaving a dispatchGroup


I have a group of objects that I have to iterate through using a for-loop and DispatchGroup. When leaving the group inside the for-loop, is calling continue necessary?

let group = DispatchGroup()

for object in objects {

    group.enter()

    if object.property == nil {
         group.leave()
         continue // does calling this have any effect even though the group is handling it?
    }

    // do something with object and call group.leave() when finished
}
group.notify(...

Solution

  • Yes, it is absolutely necessary to call continue, since you want to avoid continuing the execution of the body of your loop.

    Calling DispatchGroup.leave does not exit the current scope, you need to call continue to achieve that. leave only affects whatever you are doing with the DispatchGroup - so consequent notify or wait calls.