1

The use of invalidateAndCancel() comes up with another problem which is to reinitialize the session again if u need to use it again. After searching on this problem i got the answer of Cnoon's Alamofire Background Service, Global Manager? Global Authorisation Header?. Tried creating the manager but when it is called it never re-initialize the value, to anticipate this problem i did a small cheat i tried giving my session new name each time.

    func reset() -> Manager {

            let configuration: NSURLSessionConfiguration = {
                let identifier = "com.footbits.theartgallery2.background-session\(sessionCount)"
                let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
                return configuration
            }()

            return Alamofire.Manager(configuration: configuration)
        }


func cancelAllRequests() {

   manager.session.invalidateAndCancel()
   sessionCount += 1
   manager = reset()

}

Now i am curious of what is happening in background like the previous session is still there or has been removed by alamofire because if it is still there how many session can i create and what will be the after affect? because i tried:

manager.session.resetWithCompletionHandler {

            print("Session reset")
        }

it doesnt work.

Community
  • 1
  • 1
MQ.
  • 365
  • 6
  • 28
  • Because at that particular moment i want it to be finished and there is a possibility that i'll start with a new request the next time. – MQ. Aug 26 '16 at 00:34
  • So i already tried that it never comes into effect immediately. I dont know what is the science behind this. Also, Cnoon's answer: http://stackoverflow.com/questions/32999892/ios-alamofire-stop-all-requests encourages us to do that through invalidating the session and yes it works as well. Can u share something like how u r recommending it ? – MQ. Aug 26 '16 at 04:27

1 Answers1

2

I know this question is old, but this info will apply to you (the reader) wether or not you are using Alamofire.

You can not revalidate a URLSession, for good reason: The tasks within a session will be told to cancel upon invalidation of the session. However, those tasks may take a while to actually cancel and leave the session. To prove this, cancel a session and retrieve its tasks periodically afterwards. They'll slowly drop towards zero.

What you need to do instead is create a new session with a new identifier. Using the same identifier will cause the old session to be used and crash if new tasks are scheduled to it!

For those seeking to perform background downloads consider using a UUID().uuidString as part of the identifier and storing the id somewhere on the device (Userdefaults come to mind), and generating a new uuid after every call to invalidate your current session. Then create a new session object with a new uuid and reference that instead. Bonus: This new session immediately will have 0 open tasks which proves useful if your logic is based on that.

Example from within my project:

@UserDefaultOptional(defaultValue: nil, "com.com.company.sessionid")
private var sessionID: String?

// MARK: - Init

private var session: URLSession!

private override init() {
    super.init()
    setupNewSession()
}

private func setupNewSession() {
    let id = sessionID ?? UUID().uuidString
    sessionID = id
    let configuration = URLSessionConfiguration.background(withIdentifier: id)
    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}

private func invalidateSession() {
    session.invalidateAndCancel()
    sessionID = nil
    setupNewSession()
}
Justin Ganzer
  • 582
  • 4
  • 15