URLSession

class URLSession : NSObject

To import the NSURLSession category:

use_frameworks! pod CancelForPromiseKit/Foundation

Or NSURLSession is one of the categories imported by the umbrella pod:

use_frameworks! pod CancelForPromiseKit

And then in your sources:

import PromiseKit import CancelForPromiseKit

  • Example usage with explicit cancel context:

    let context = firstly {
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.compactMap { data, _ in
        try JSONSerialization.jsonObject(with: data) as? [String: Any]
    }.then { json in
        //…
    }.cancelContext
    //…
    context.cancel()
    

    Example usage with implicit cancel context:

    let promise = firstly {
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.compactMap { data, _ in
        try JSONSerialization.jsonObject(with: data) as? [String: Any]
    }.then { json in
        //…
    }
    //…
    promise.cancel()
    

    We recommend the use of OMGHTTPURLRQ which allows you to construct correct REST requests:

    let context = firstly {
        let rq = OMGHTTPURLRQ.POST(url, json: parameters)
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.then { data, urlResponse in
        //…
    }.cancelContext
    //…
    context.cancel()
    

    We provide a convenience initializer for String specifically for this promise:

    let context = firstly {
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.compactMap(String.init).then { string in
        // decoded per the string encoding specified by the server
    }.then { string in
        print("response: string")
    }
    //…
    context.cancel()
    

    Other common types can be easily decoded using compactMap also:

    let context = firstly {
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.compactMap {
        UIImage(data: $0)
    }.then {
        self.imageView.image = $0
    }
    //…
    context.cancel()
    

    Though if you do decode the image this way, we recommend inflating it on a background thread first as this will improve main thread performance when rendering the image:

    let context = firstly {
        URLSession.shared.dataTaskCC(.promise, with: rq)
    }.compactMap(on: QoS.userInitiated) { data, _ in
        guard let img = UIImage(data: data) else { return nil }
        _ = cgImage?.dataProvider?.data
        return img
    }.then {
        self.imageView.image = $0
    }
    //…
    context.cancel()
    

    See also

    OMGHTTPURLRQ

    Remark

    We deliberately don’t provide a URLRequestConvertible for String because in our experience, you should be explicit with this error path to make good apps.

    Declaration

    Swift

    public func dataTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible) -> CancellablePromise<(data: Data, response: URLResponse)>

    Parameters

    convertible

    A URL or URLRequest.

    Return Value

    A cancellable promise that represents the URL request.

  • Wraps the (Data?, URLResponse?, Error?) response from URLSession.uploadTask(with:from:) as CancellablePromise<(Data,URLResponse)>

    Declaration

    Swift

    public func uploadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, from data: Data) -> CancellablePromise<(data: Data, response: URLResponse)>
  • Wraps the (Data?, URLResponse?, Error?) response from URLSession.uploadTask(with:fromFile:) as CancellablePromise<(Data,URLResponse)>

    Declaration

    Swift

    public func uploadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, fromFile file: URL) -> CancellablePromise<(data: Data, response: URLResponse)>
  • Wraps the URLSesstionDownloadTask response from URLSession.downloadTask(with:) as CancellablePromise<(URL,URLResponse)>

    Remark

    we force a to parameter because Apple deletes the downloaded file immediately after the underyling completion handler returns.

    Declaration

    Swift

    public func downloadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, to saveLocation: URL) -> CancellablePromise<(saveLocation: URL, response: URLResponse)>