Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All requests made with Alamofire manager cancelled after adding InterceptingProtocol #5

Closed
mateuszbuda opened this issue Aug 2, 2015 · 9 comments
Assignees

Comments

@mateuszbuda
Copy link

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})
@mateuszbuda
Copy link
Author

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)

@akashivskyy
Copy link
Collaborator

Thanks for noticing! 🍷

@akashivskyy akashivskyy added the bug label Aug 9, 2015
@AnthonyMDev
Copy link

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

@AnthonyMDev
Copy link

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.

@mateuszbuda
Copy link
Author

Are you using anything else that do something with protocolClasses?

@AnthonyMDev
Copy link

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

@AnthonyMDev
Copy link

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.

@akashivskyy akashivskyy reopened this Aug 13, 2015
@akashivskyy
Copy link
Collaborator

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

@akashivskyy
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants