0

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)
} 
Alex Zavatone
  • 4,106
  • 36
  • 54
Ananth
  • 31
  • 1
  • 4

1 Answers1

0

The basic idea behind this is that your program supports background mode, so you need to correctly support you connections. You know that after a certain period of time in background, your program goes into suspend mode. So whenever you app goes to background, you can ask additional time from the system and check amount of time the app has to run in the background to finish or close all your connections. Check these methods:

begin​Background​Task(with​Name:​expiration​Handler:​)

background​Time​Remaining

The problem is when your app wakes up after suspend, it continues exactly from the same point where it stopped. So you can get all kinds of errors, such as session or connection timeout, for example.

alx
  • 74
  • 3
  • Thanks. I try to be more clear with my question. When the image is selected, The PictureUpload class is initialised. Next screen when the user taps on the upload the internet is checked and if there is no connection, the Image is saved and later uploaded when there is connection. The issue is when the app is moved background and not completely closed. If Internet is active and App is relaunched it executes the delegate methods of the PictureUpload. It starts with executing the OfflinePictureUpload class and then when it has to execute the Delegate, it shifts to the PictureUpload class. – Ananth Mar 29 '17 at 08:11
  • Thanks a lot. I have managed to do it. I have changed the background identifier. I have just noticed that both the URLTasks has the same background identifier. So, I changing the identifier did the trick. – Ananth Mar 29 '17 at 08:25