8

I use this code.

var apiPath : String = "/api/list/"
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 60
let manager = Alamofire.Manager(configuration: configuration)
manager.session.configuration.HTTPAdditionalHeaders = ["_token" : self._token]
manager.request(.GET, self._host + self._url + apiPath, parameters: nil)
    .responseSwiftyJSON ({ (request, response, resultJson, error) in
        if (resultJson["Success"]) {
             //get list success
        } else {
            println("request : \(request)")
            println("response : \(response)")
            println("resultJson : \(resultJson)")
            println("error : \(error)")
        }

})

And I got some problem

Alamofire version 1.2.1 : No Problem

Alamofire version 1.2.2 & 1.2.3 :

request : { URL: https://test.com/api/list/ }

response : nil

resultJson : null

error : Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7feb92c434f0 {NSErrorFailingURLKey=https ://test.com/api/list/, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http s://test.com/api/list/})

why response was nil and resultJson was null on version 1.2.2 and 1.2.3 Please help me what problem in this code..

Torres Fernando
  • 113
  • 1
  • 8

2 Answers2

16

I just encountered the same problem as you today after updating Alamofire from 1.2.1 to 1.2.3.

I discovered by adding "manager.session.invalidateAndCancel()" at the end and inside the responseJSON block fixed this issue. But what I cannot get my head around is that how can this line of code INSIDE the responseJSON block affects the responseJSON results.

Anyway I will just run with this fix until the Alamofire team fixes it or someone explains to me why this is happening.

user4781334
  • 374
  • 2
  • 8
  • could you have a look at my code? I still can't fix it... http://stackoverflow.com/questions/31289074/authenticated-http-request-swift-alamofire?noredirect=1 – CularBytes Jul 08 '15 at 19:38
  • 3
    This is because this line with make sure the manager is retained until the inner block has been executed. Basically you created a retain cycle to 'fix' the problem. The real problem is that the manager is being released before it reached the response block with that line. You need to make sure you keep a reference to the manager. – Kevin R Dec 16 '15 at 10:02
  • @KevinR - how should you make sure you keep a reference to the manager? – raklos Jan 30 '17 at 22:01
  • 1
    @raklos you can either use the default manager (or sharedManager if using 4.0 or later), or, if you define your own configuration, make sure to keep a static reference somewhere. This way the instance is globally accessible and won't create a possible retain cycle. – Kevin R Jan 31 '17 at 09:56
1

I noticed that your API endpoint indicates to a secure connection:

httpS://test.com/api/list/

Just try it just in case, maybe it repeats your situation.

In my case, this was a typo in the API manager code. Which from the part can be said is connected with App Transport Security Settings.

Just changed the protected protocol from httpS:// to http:// and the error:

NSURLErrorDomain Code = -999 "cancelled"

was gone and it all worked!

+And also if you had a similar problem. Be sure to discuss this with a backend specialist who deals with the server or API configuration for your application. This means that the server does not have valid security certificates. Perhaps you still need a secure connection. Or this specialist can again configure everything back from http:// to httpS://, and I'm not sure (did not check) whether this will work again when in the code you are already using a non-secure http:// connection.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
CAHbl463
  • 102
  • 7