Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DispatchGroup: check how many "entered"

I'm using Swift 3 DispatchGroup to wait until multiple async operations are finished (according to this answer which works perfect and as expected.

Is there a way to check how many operations are entered already, like dispatchGroup.count or something like that?

like image 684
swalkner Avatar asked Aug 10 '17 09:08

swalkner


People also ask

How does a DispatchGroup work?

You attach multiple work items to a group and schedule them for asynchronous execution on the same queue or different queues. When all work items finish executing, the group executes its completion handler. You can also wait synchronously for all tasks in the group to finish executing.

Why is DispatchGroup used in certain situations?

DispatchGroup allows for aggregate synchronization of work. It can be used to submit multiple different work items or blocks and track when they all complete, even though they might run on different queues.


1 Answers

You can see counts of enters to the group in debug description:

OS_dispatch_group: group[0x60000221d950] = { xref = 3, ref = 3, count = 2, gen = 0, waiters = 0, notifs = 1 }

and extract int value out of it:

let count = dispatchGroup.debugDescription.components(separatedBy: ",").filter({$0.contains("count")}).first?.components(separatedBy: CharacterSet.decimalDigits.inverted).compactMap{Int($0)}.first
like image 140
Farid A. Avatar answered Oct 14 '22 17:10

Farid A.