SessionManager

open class SessionManager

Responsible for creating and managing Request objects, as well as their underlying NSURLSession.

  • A default instance of SessionManager, used by top-level Alamofire request methods, and suitable for use directly for any ad hoc requests.

    Declaration

    Swift

    open static let `default`: SessionManager =
  • Creates default values for the Accept-Encoding, Accept-Language and User-Agent headers.

    Declaration

    Swift

    open static let defaultHTTPHeaders: HTTPHeaders =
  • Default memory threshold used when encoding MultipartFormData in bytes.

    Declaration

    Swift

    open static let multipartFormDataEncodingMemoryThreshold: UInt64 = 10_000_000
  • The underlying session.

    Declaration

    Swift

    open let session: URLSession
  • The session delegate handling all the task and session delegate callbacks.

    Declaration

    Swift

    open let delegate: SessionDelegate
  • Whether to start requests immediately after being constructed. true by default.

    Declaration

    Swift

    open var startRequestsImmediately: Bool = true
  • The request adapter called each time a new request is created.

    Declaration

    Swift

    open var adapter: RequestAdapter?
  • The request retrier called each time a request encounters an error to determine whether to retry the request.

    Declaration

    Swift

    open var retrier: RequestRetrier?
  • The background completion handler closure provided by the UIApplicationDelegate application:handleEventsForBackgroundURLSession:completionHandler: method. By setting the background completion handler, the SessionDelegate sessionDidFinishEventsForBackgroundURLSession closure implementation will automatically call the handler.

    If you need to handle your own events before the handler is called, then you need to override the SessionDelegate sessionDidFinishEventsForBackgroundURLSession and manually call the handler when finished.

    nil by default.

    Declaration

    Swift

    open var backgroundCompletionHandler: (() -> Void)?
  • Creates a DownloadRequest from the resumeData produced from a previous request cancellation to retrieve the contents of the original request and save them to the destination.

    If destination is not specified, the contents will remain in the temporary location determined by the underlying URL session.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    open func download(
            resumingWith resumeData: Data,
            to destination: DownloadRequest.DownloadFileDestination? = nil)
            -> DownloadRequest
  • Encodes multipartFormData using encodingMemoryThreshold and calls encodingCompletion with new UploadRequest using the url, method and headers.

    It is important to understand the memory implications of uploading MultipartFormData. If the cummulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.

    The encodingMemoryThreshold parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of the MultipartFormData is below the encodingMemoryThreshold, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    open func upload(
            multipartFormData: @escaping (MultipartFormData) -> Void,
            usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
            to url: URLConvertible,
            method: HTTPMethod = .post,
            headers: HTTPHeaders? = nil,
            encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
  • Encodes multipartFormData using encodingMemoryThreshold and calls encodingCompletion with new UploadRequest using the urlRequest.

    It is important to understand the memory implications of uploading MultipartFormData. If the cummulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.

    The encodingMemoryThreshold parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of the MultipartFormData is below the encodingMemoryThreshold, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    open func upload(
            multipartFormData: @escaping (MultipartFormData) -> Void,
            usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
            with urlRequest: URLRequestConvertible,
            encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)