Starting with Managers in Alamofire

In this lesson

Starting with managers in Alamofire is easy as pie. And there are several configurations and customizations immediately accessible when you do so. Let’s talk through some examples like setting the default thread to run your requests, setting the HTTP headers, and other default behaviors.

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

0:11

Kyle here with brax.tv and we’re going to talk about using managers when working with Alamofire. Now there’s a class just called Manager in Alamofire which we can access to set some defaults and custom configuration when creating requests. So I’m just going to walk through a couple of the things we can do with that. 

0:34

First and foremost, here’s an example of how we can show how we can set some default HTTP headers for our manager. And to do that, I’m just grabbing the default headers from the standard AlamofireManager. And digging down through a big hierarchy just to get to these headers and I’m adding the accepted content types and setting that application/json here for the default headers for all requests created with this manager. And that’s okay since every request I’m using this app is actually returning JSON. If we make another request that’s not going to return JSON, we may have to modify this later.

1:15

And then to set these new default headers to our manager, we’re actually going to have to create an NSURLSessionConfiguration here. And set that configuration’s additional HTTP headers to the default headers that we created about. And then we are saving the NetworkInterface’s custom manager to a brand new manager created with this configuration that we test initialized above. So now all methods within the scope of this class, and I guess anything that has access to this because it’s not a private variable, have access to this manager to use the defaults that we’ve set here. 

1:54

In addition to setting some of the HTTP defaults like we have there is something I’d like to point out with the actual initialization of this configuration. And that is when we create this NSURLSessionConfiguration, we are creating it as a background session configuration and just giving the thread identifier to run this session on. And that means that any requests run with this configuration are actually going to be run in a background thread and specifically a background thread with this name. There are a couple other options when creating a new URLSessionConfiguration, of course you can run it on whatever default thread it chooses. And there’s also something called an ephemeral session configuration which I’ve never used and I’m not sure I even know what ephemeral means in this example.

2:46

But if we ever need it, it’s there and then of course since we’re sending that configuration to this new manager. Just to restate any request created and run with this manager will have these default headers and will be run on a background thread of this name. When creating a new request, all we need to do is replace any instances of Alamofire.request with manager.request or at least the reference to our manager. Now when you call Alamofire.request, it’s going to use this default AlamofireManager’s shared instance manager to create the request. But if we create it with our own custom reference to my manager then all of those defaults that we set above will actually be applied to this request. 

Additional Info

Register to get access to additional resources and info.