RetryNetworkService
public final class RetryNetworkService : NetworkService
RetryNetworkService
can request resource. When a request fails with a given condtion it can retry the request after a given time interval.
The count of retry attemps can be configured as well.
Seealso
NetworkService
-
Creates an instance of
RetryNetworkService
Declaration
Swift
public init(networkService: NetworkService, numberOfRetries: Int, idleTimeInterval: TimeInterval, shouldRetry: @escaping (NetworkError) -> Bool, dispatchRetry: @escaping (_ deadline: DispatchTime, _ execute: @escaping () -> Void ) -> Void = { deadline, execute in DispatchQueue.global(qos: .utility).asyncAfter(deadline: deadline, execute: execute) })
Parameters
networkService
a networkservice
numberOfRetries
the number of retrys before final error
idleTimeInterval
time between error and retry
shouldRetry
closure which evaluated if error should be retry
dispatchRetry
closure where to dispatch the waiting
-
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