title: How can I cancel the URLSession.shared.dataTask if I exit the page before it completes?
body: <p>Following the recommendation from <a href="https://stackoverflow.com/a/27712427/3286489">https://stackoverflow.com/a/27712427/3286489</a>, I can perform asynchronous download as below, and it is called during <code>viewDidLoad</code>.</p>
  <pre><code>    func loadItems(tuple : (name : String, imageURL : URL)) {
          print(&quot;Download Started&quot;)
          URLSession.shared.dataTask(with: tuple.imageURL, completionHandler :
          { data, response, error in
              guard let data = data, error == nil else { return }
              print(response?.suggestedFilename ?? tuple.imageURL.lastPathComponent)
              print(&quot;Download Finished&quot;)
              DispatchQueue.main.async() { [weak self] in
                  self?.displayFlag(data: data, title: tuple.name)
              }
          }).resume()
      }
  </code></pre>
  <p>This all works well. My question is, in the event of a slow download, and I exit the page, how and where (<code>viewDidUnload</code>? looks that that is deprecated already) can I cancel the fetching (image downloading) task?</p>