title: Swift: How to deinitialize a URLSession when other URLSession is initialized?
body: <p>I have a <code>URLSessionDataDelegate</code> to upload a picture to a server and the following is one part of it.</p>
  
  <p>The <code>URLSession</code> is initialized immediately when I select an image to be uploaded. </p>
  
  <p>But if the user taps on the <code>Upload</code> button and if there is <code>no internet connection</code>, I am saving the image in Realm and uploading it next time when the app is launched. </p>
  
  <p>For uploading the saved image, I have the created another class called <code>OfflinePictureUploadClass</code> of almost identical code. </p>
  
  <p>If the app has been quit by swipe killing the app and then launched again, it is executing the correct <code>URLSessionDataDelegate</code> method of the <code>OfflinePictureUploadClass</code>. So, there is no issue in this case.</p>
  
  <p>But, if the app is relaunched normally without completely closing it, it is executing the delegate methods of the main <code>PictureUpload</code> class and then the upload is failing. </p>
  
  <p>So, how can I overcome this, how do I deinitialize the <code>URLSessionDataDelegate</code>, that is initialized when the image is selected.</p>
  
  <p>The code below is just to show what is initialized when an image is selected. </p>
  
  <pre><code>fileprivate var DefaultSession: Foundation.URLSession!
  fileprivate var BackgroundSession: Foundation.URLSession!
  
  override init() {
      super.init()
      // default session to fetch
      let config = URLSessionConfiguration.default
      config.timeoutIntervalForRequest = 20
      DefaultSession = Foundation.URLSession(configuration: config, delegate: nil, delegateQueue: .main)
      // background session
      let backgroundConfiguration = "com.xxx.backgroundconfiguration.upload"
      let configuration = URLSessionConfiguration.background(withIdentifier: backgroundConfiguration)
      BackgroundSession = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
  } 
  </code></pre>