I have a URLSessionDataDelegate
to upload a picture to a server and the following is one part of it.
The URLSession
is initialized immediately when I select an image to be uploaded.
But if the user taps on the Upload
button and if there is no internet connection
, I am saving the image in Realm and uploading it next time when the app is launched.
For uploading the saved image, I have the created another class called OfflinePictureUploadClass
of almost identical code.
If the app has been quit by swipe killing the app and then launched again, it is executing the correct URLSessionDataDelegate
method of the OfflinePictureUploadClass
. So, there is no issue in this case.
But, if the app is relaunched normally without completely closing it, it is executing the delegate methods of the main PictureUpload
class and then the upload is failing.
So, how can I overcome this, how do I deinitialize the URLSessionDataDelegate
, that is initialized when the image is selected.
The code below is just to show what is initialized when an image is selected.
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)
}