netguru / ResponseDetective

Sherlock Holmes of the networking layer. :male_detective:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

All requests made with Alamofire manager cancelled after adding InterceptingProtocol

mateuszbuda opened this issue · comments

After I've added ResponseDetective to my project and assigned InterceptingProtocol to protocol classes of configuration used to create Alamofire manager

configuration.protocolClasses = [InterceptingProtocol.self]
manager = Alamofire.Manager(configuration: configuration)

all my requests are canceled.

ResponseDetective prints requests and responses correctly, but Alamofire manager response callback is always called with the following error:

Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f9683068010 {NSErrorFailingURLKey=http://private-c73ea-roommatev2.apiary-mock.com/sessions, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http://private-c73ea-roommatev2.apiary-mock.com/sessions})

Solution found: #6
Just needed to append InterceptingProtocol to protocol classes and not assigning new array like suggested in Usage

configuration.protocolClasses?.insert(InterceptingProtocol.self, atIndex: 0)

Thanks for noticing! 🍷

I'm having this problem while using the new suggested way of adding the protocol class with Alamofire.

I fixed this by using append rather than insert: atIndex:

Edit: Scratch that. This doesn't fix it. This allows the requests to work, but stops intercepting the requests.

Are you using anything else that do something with protocolClasses?

Here is the code that is setting up my configuration:

  let networkManager: Manager = {
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = Manager.defaultHTTPHeaders

    configuration.protocolClasses = map(configuration.protocolClasses, { (var protocolClasses) in
      protocolClasses.insert(InterceptingProtocol.self, atIndex: 0)
      return protocolClasses
    }) ?? [InterceptingProtocol.self]

    return Manager(configuration: configuration, serverTrustPolicyManager: nil)
  }()

The configuration's protocolClasses array contains:

ResponseDetective.InterceptingProtocol,
_NSURLHTTPProtocol,
_NSURLDataProtocol,
_NSURLFTPProtocol,
_NSURLFileProtocol,
NSAboutURLProtocol

I'm going to continue attempting to debug this this morning, but I'm pretty lost at the moment.
It may be worth noting that I'm making HTTPS requests. I know there were issues in the past with that.

I'm also using the commit tagged for version 0.2

It seems that stopLoading is being called, which is canceling the request. But when I check out the response in the debugger, I see that the response from the server is there. I just also have an error NSURLErrorDomain code=-999 "canceled"
Because there is an error, my validation is failing.

ResponseDetective is also never logging the responses.

@AnthonyMDev I will investigate that. Could you provide me with a sample project?

Hi everyone, I came back to this issue recently and found out that cancellation happens because of Alamofire.Manager.deinit which invalidates the session, which triggers a chain reaction of cancelling all requests.

The following code:

let manager = Alamofire.Manager()
manager.request(.GET, "https://httpbin.org/get").response { _, _, _, error in
    print(error)
}

will always fail with NSURLErrorCancelled code, even with ResponseDetective disabled, because manager is deallocated before the request even starts loading.

In order for Manager to complete the tasks successfully, its instance needs to be retained (e.g. in a private property). This also makes ResponseDetective work again.

I'm closing this issue as it has been tested with retained Alamofire.Manager in the upcoming 0.4 release.