BasicNetworkService

public final class BasicNetworkService : NetworkService

BasicNetworkService handles network request for resources by using a given NetworkAccess.

Example:

// Just use an URLSession for the networkAccess.
let basicNetworkService: NetworkService = BasicNetworkService(networkAccess: URLSession(configuration: .default))

Seealso

NetworkService
  • Creates an BasicNetworkService instance with a given network access to execute requests on.

    Declaration

    Swift

    public init(networkAccess: NetworkAccess)

    Parameters

    networkAccess

    provides basic access to the network.

  • Fetches a resource asynchronously from remote location. Execution of the requests starts immediately. Execution happens on no specific queue. It dependes on the network access which queue is used. Once execution is finished either the completion block or the error block gets called. You decide on which queue these blocks get executed.

    Example:

    let networkService: NetworkService = //
    let resource: Resource<String> = //
    
    networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in
       print(htmlText, response)
    }, onError: { error in
       // Handle errors
    })
    

    Declaration

    Swift

    @discardableResult
    public func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void,
                        onError: @escaping (NetworkError) -> Void) -> NetworkTask

    Parameters

    queue

    The DispatchQueue to execute the completion and error block on.

    resource

    The resource you want to fetch.

    onCompletionWithResponse

    Callback which gets called when fetching and transforming into model succeeds.

    onError

    Callback which gets called when fetching or transforming fails.

    Return Value

    a running network task