Using DispatchGroup() in Swift 3 to perform a task?
<p>With Swift 3, using GCD has changed to <code>DispatchGroup()</code>, and I'm trying to learn to use it in my code.</p>

<p>Currently I have a function in another class that attempts to download a file and output its speed. I like to have that function finish first because I assign its speed to a <code>var</code> that I will be using in the first class to perform other tasks that is dependent upon that <code>var</code>.</p>

<p>It goes something like this:</p>

<p>Second class:</p>

<pre><code>func checkSpeed()
{
    // call other functions and perform task to download file from link

    // print out speed of download

    nMbps = speedOfDownload
}
</code></pre>

<p>First Class:</p>

<pre><code>let myGroup = DispatchGroup()
let check: SecondClass = SecondClass()

myGroup.enter()

check.checkSpeed()

myGroup.leave()

myGroup.notify(queue: DispatchQueue.main, execute: {

    print("Finished all requests.")

    print("speed = \(check.nMbps)")
})
</code></pre>

<p>The problem is <code>Finish all requests</code> gets output first, thus returning <code>nil</code> for <code>speed</code>, then afterwards <code>checkSpeed</code> finishes and outputs the correct download speed.</p>

<p>I believe I'm doing this wrong, but I'm not sure?</p>

<p>How can I ensure that <code>speed</code> obtains the correct value after the completion of <code>checkSpeed</code> in my first class?</p>

<p>The details of <code>checkSpeed</code> is exactly the same as <code>connectedToNetwork</code> from GitHub: <a href="https://github.com/rldaulton/connectedness/blob/master/connectedness.swift" rel="noreferrer">connectedness.swift</a></p>