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
queueThe
DispatchQueueto execute the completion and error block on.resourceThe resource you want to fetch.
onCompletionWithResponseCallback which gets called when fetching and transforming into model succeeds.
onErrorCallback 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) -> NetworkTaskParameters
queueThe
DispatchQueueto execute the completion block on. Defaults tomain.resourceThe resource you want to fetch.
onCompletionWithResponseCallback 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
mainqueue.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) -> NetworkTaskParameters
resourceThe resource you want to fetch.
onComplitionCallback 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) -> NetworkTaskParameters
resourceThe resource you want to fetch.
onComplitionCallback which gets called when fetching and transforming into model succeeds.
onErrorCallback 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) -> NetworkTaskParameters
resourceThe resource you want to fetch.
onCompletionCallback which gets called when fetching and transforming into model succeeds.
onErrorCallback which gets called when fetching or transforming fails.
Return Value
a running network task
View on GitHub
NetworkService Protocol Reference