Cancel a completion block
<p>Quick question. </p>

<p>Usually, we make web call and on obtaining response, we return the data in a completion block as below</p>

<pre><code>func someAPIcall(input: input, completion: (result: data) -&gt; Void) {
    ...
    completion(data)
}
</code></pre>

<p>and I make use of the function as below</p>

<pre><code>someAPIcall(input) {
    (result: data) in
    print(result)
    // Update UI, etc
}
</code></pre>

<p>Is it possible to cancel the completion block somehow, at any later point of time? Say, if I make the web call and <code>popViewController</code> immediately, though if I cancel the <code>request</code>, if the data is returned to the completion block, the completion task is executed.</p>

<p>Is there any mechanism by which I could assign a <code>var</code> to the closure and could cancel that later?</p>

<p>How could I cancel the block from executing when I require, say in <code>viewWillDisappear</code>? </p>