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

Adding Headers with ephemeralSessionConfiguration() #692

Closed
Jeanblanc88 opened this issue Aug 16, 2015 · 6 comments
Closed

Adding Headers with ephemeralSessionConfiguration() #692

Jeanblanc88 opened this issue Aug 16, 2015 · 6 comments

Comments

@Jeanblanc88
Copy link

Hi,

I am using the code you are suggesting on both:

  • using ephemeral sessions
  • adding header to any query

The problem is that mixing both (adding query to ephemeral sessions), does not work. I checked :

let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let manager = Manager(configuration: configuration)
manager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer \(accessToken!)" ]

Doing this does not add the header.
Is there something I am missing?

@cnoon
Copy link
Member

cnoon commented Aug 16, 2015

I just added some tests in 10f9478 to verify that this is working as expected. The tests are passing without any issues on iOS 9. I currently have an Xcode 7 issue where none of my iOS 8 simulators are working so I can't verify the tests there.

Could you provide some additional details around your issue?

  • Which version of Xcode are you using?
  • Which branch, tag or commit are you build Alamofire on?
  • How did you build Alamofire?

Also, you should be adding the headers to the configuration before you create the Manager.

@cnoon cnoon self-assigned this Aug 16, 2015
@cnoon cnoon added this to the 2.0.0 milestone Aug 16, 2015
@tkrajacic
Copy link

@cnoon With the latest tests you added, Carthage can't compile Alamofire anymore because

ManagerTests.swift:127:63: error: 'backgroundSessionConfigurationWithIdentifier' is only available on OS X 10.10 or newer

Interestingly I am using 10.10.5 and Xcode 7b5 toolchain when using carthage update

@Jeanblanc88
Copy link
Author

Thank you for helping out.
Below the answers:

  • Which version of Xcode are you using? > Xcode 6.4
  • Which branch, tag or commit are you build Alamofire on? > Installed Alamofire yesterday. Using v 1.3.1
  • How did you build Alamofire? > I used cocoapods following guidelines here: https://github.com/Alamofire/Alamofire#cocoapods.
  • Extra: running app on iOS 8.4

I previously just imported Alamofire.swift file into the project but that did not work so it led to install Cocoapods (thought that this would solve the issue, but that was not the case).

Some more information:

  • I adjusted the code following your suggestion :
    let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
    configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer (accessToken!)" ]
    let manager = Manager(configuration: configuration)

The result I get is:
REQUEST: <NSMutableURLRequest: 0x7f977b910cd0> { URL: https://www.custom.url/api/v1/custom.json }
RESPONSE: nil
DATA: nil
ERROR: Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f977b8690c0 {NSErrorFailingURLKey=https://www.custom.url/api/v1/custom.json, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://www.custom.url/api/v1/custom.json})

  • When I just send the headers, the "Cache-Control" = "must-revalidate, private, max-age=0" part of my answer seem to be ignored, since redoing the query with other parameters returns the same answer. This led me to force the session not to cache the result by using ephemeralSessionConfiguration, but as you can see it does not work.
  • Because of this problem, I also tried removing all cached responses with NSURLCache.sharedURLCache().removeAllCachedResponses() before making any new request, but it did not work as well.

@Jeanblanc88
Copy link
Author

So I managed to get the result by retaining the 'manager' as explained in #157

However, I still have that caching problem which was the initial problem that led me to use customer Manager. I am using ephemeralSessionConfiguration(), but the request is still stored.

Details points:

  • When I just send the headers, the "Cache-Control" = "must-revalidate, private, max-age=0" part of my answer seem to be ignored, since redoing the query with other parameters returns the same answer. This led me to force the session not to cache the result by using ephemeralSessionConfiguration, but as you can see it does not work.
  • I also tried removing all cached responses with NSURLCache.sharedURLCache().removeAllCachedResponses() before making any new request, but it did not work as well.

Thank you!

@cnoon
Copy link
Member

cnoon commented Aug 17, 2015

Thanks for pointing the issue out @tkrajacic. I fixed the issue in bf86ff5. The fix was terrible to get right. The #available checks are not working as expected and I had to switch over to using an OS predefine. If anyone else can find a better solution here I'm all ears.

@cnoon
Copy link
Member

cnoon commented Aug 17, 2015

@Jeanblanc88 I'm glad you figured out your manager instance was not being retained.

As for your issue now, using an ephemeral background session does not disable caching. It only switches it over to using in-memory caching. To disable caching, you should set the URLCache property on the configuration to nil.

I'm going to close this issue out since it has been resolved. If you have any further issues with the caching behavior, I'd encourage you to open a question on Stack Overflow.

@cnoon cnoon closed this as completed Aug 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants