Custom SessionManager in Alamofire 4.7 cancels immediately
Asked Answered
H

4

5

Hi I'm trying to create a custom SessionManager in Alamofire in order to change the default timeoutIntervalForRequest value. I'm using the code below:

  let configuration=URLSessionConfiguration.default
  configuration.timeoutIntervalForRequest=20
  let sessionManager=Alamofire.SessionManager(configuration:configuration)

  sessionManager.request("my url", method: .post, parameters: params, encoding: JSONEncoding.default, headers: header)
        .responseJSON(completionHandler: { (response) in
                  if response.result.isSuccess{
                     //here goes the rest of my code
                   }
            }
               else{
                    //here goes the connection error part
                   }
            })

The problem is I'm always getting the error part and when I print the response in error part, it looks like this:

finished with error - code: -999

FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled"

as if my request immediately cancels. If I change the sessionManager to Alamofire that is the default manager, it works fine but I need to change timeoutIntervalForRequest value. I'm using Xcode 9.3 and swift 4 and Alamofire 4.7.2 . Any Suggestions?

Hazlett answered 26/5, 2018 at 8:59 Comment(0)
B
9

I found a solution in Alamofire repository issues

jshier commented on Oct 10, 2016

An unexpected error -999 almost always means your SessionManager was deallocated, cancelling any ongoing requests. I suggest you create a singleton value for your custom SessionManager, or perhaps just reevaluate if you really need one.

if you create a singleton value for your object it remains in memory and prevent from deallocate

and another thing that i avoid is to name your variables diffrent, a sessionManager is in Alamofire and your variable is also called sessionManager.

Alamofire 4.7 , Swift 4

import Alamofire

class Networking {

  public static let sharedManager: SessionManager = {
      let configuration = URLSessionConfiguration.default
      configuration.timeoutIntervalForRequest=20
      let manager = Alamofire.SessionManager(configuration: configuration, delegate: SessionManager.default.delegate)
      return manager
  }()
}

Alamofire 5.0.0-beta.6 , Siwft 5.1

import Alamofire

class Networking {

  static let APIManager: Session = {
      let configuration = URLSessionConfiguration.default
      configuration.timeoutIntervalForRequest = 20
      let delegate = Session.default.delegate
      let manager = Session.init(configuration: configuration,
                                 delegate: delegate,
                                 startRequestsImmediately: true,
                                 cachedResponseHandler: nil)
      return manager
  }()
}
Byelaw answered 3/1, 2019 at 10:9 Comment(2)
This code gives an error : Static properties may only be declared on a type. what should we do for that.Karankaras
@Karankaras I forgot to include whole code, This means that you cant create static variables globally. Updated answerByelaw
O
2

I solved this issue by adding additional headers to my custom Alamofire session manager:

static let sessionManager: Alamofire.SessionManager = {
        let config = URLSessionConfiguration.default
        config.timeoutIntervalForRequest = 10
        config.timeoutIntervalForResource = 10
        config.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders

        return Alamofire.SessionManager(configuration: config) 
}()

Hope this helps!

Offcolor answered 17/1, 2020 at 9:6 Comment(0)
T
1

You can try this. I hope it helps, it worked for me:

Global variable

private static var alamofireManager: SessionManager?

Within your perform method using defer to terminate the session at the end of the scope

let configuration = URLSessionConfiguration.default     
configuration.timeoutIntervalForRequest = timeout //Any timeout value you want
let alamofireManager = SessionManager(configuration: configuration)
self.alamofireManager = alamofireManager

defer {
 self.alamofireManager?.session.finishTasksAndInvalidate()
 self.alamofireManager?.session.invalidateAndCancel()        
}

self.alamofireManager?.request(/* Your request parameters*/)
Threesome answered 14/3, 2024 at 17:44 Comment(0)
G
0

Most probably your SessionManager was deallocated, cancelling any ongoing requests. try to assign the session manager instance to a property.

check https://github.com/Alamofire/Alamofire/issues/1684

Grandma answered 15/4, 2022 at 12:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.