NetworkService
public protocol NetworkService
NetworkService
provides access to remote resources.
Seealso
BasicNetworkService
Seealso
NetworkServiceMock
-
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 func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void,
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
-
request(queue:resource:onCompletionWithResponse:)
Extension methodFetches 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 the completion block gets called. You decide on which queue completion gets executed. Defaults to
main
.Example:
let networkService: NetworkService = // let resource: Resource<String> = // networkService.request(resource: resource, onCompletionWithResponse: { result in print(result) })
Declaration
Swift
@discardableResult func request<Result>(queue: DispatchQueue = .main, resource: Resource<Result>, onCompletionWithResponse: @escaping (Swift.Result<(Result, HTTPURLResponse), NetworkError>) -> Void) -> NetworkTask
Parameters
queue
The
DispatchQueue
to execute the completion block on. Defaults tomain
.resource
The resource you want to fetch.
onCompletionWithResponse
Callback which gets called when request completes.
Return Value
a running network task
-
request(_:onCompletion:)
Extension methodFetches 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 the completion block gets called. Completion gets executed on
main
queue.Example:
let networkService: NetworkService = // let resource: Resource<String> = // networkService.request(resource, onCompletion: { result in print(result) })
Declaration
Swift
@discardableResult func request<Result>(_ resource: Resource<Result>, onCompletion: @escaping (Swift.Result<Result, NetworkError>) -> Void) -> NetworkTask
Parameters
resource
The resource you want to fetch.
onComplition
Callback which gets called when request completes.
Return Value
a running network task
-
request(_:onCompletion:onError:)
Extension methodFetches 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. These blocks are called on the main queue.
Example:
let networkService: NetworkService = // let resource: Resource<String> = // networkService.request(resource, onCompletion: { htmlText in print(htmlText) }, onError: { error in // Handle errors })
Declaration
Swift
@discardableResult func request<Result>(_ resource: Resource<Result>, onCompletion: @escaping (Result) -> Void, onError: @escaping (NetworkError) -> Void) -> NetworkTask
Parameters
resource
The resource you want to fetch.
onComplition
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
-
request(_:onCompletionWithResponse:onError:)
Extension methodFetches 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. These blocks are called on the main queue.
Example:
let networkService: NetworkService = // let resource: Resource<String> = // networkService.request(resource, onCompletionWithResponse: { htmlText, httpResponse in print(htmlText, httpResponse) }, onError: { error in // Handle errors })
Declaration
Swift
@discardableResult func request<Result>(_ resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void, onError: @escaping (NetworkError) -> Void) -> NetworkTask
Parameters
resource
The resource you want to fetch.
onCompletion
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