ResourceRefresher Class Reference
public class ResourceRefresher<Resource> where Resource : Decodable, Resource : Encodable
An object that refreshes a single resource at regular intervals.
Refresh is requested by the user of ths class, but it’s ignored unless the required intervals have passed.
The resource is cached locally and it’s re-read only on initialization or when subscribing onResourceLoaded.
You can listen to resource updates by using the onResourceLoaded observable.
This will first push an event with a resource read from disk, if present, and then all refreshes from remote.
-
The unique identifier for this resource refresher.
Declaration
Swift
public var id: String { get } -
An observable sequence of resources, starting from whatever might be cached on disk, and followed with all the subsequent successful refreshes.
Declaration
Swift
public var onLatestResource: Observable<Resource> { get } -
An observable that emits whenever a load completes either successfully or not.
Declaration
Swift
@ReplaySubject <Void> public var onLoadCompleted: Observable<Void> { get } -
An observable sequence of errors that might come from network or from failing to write the cached resource on disk.
Note that 304 errors are ignored as they are the intended network response when the resource was not modified.
Declaration
Swift
@Subject <any Error> public var onRefreshError: Observable<any Error> { get } -
Creates a resource refresher with the specified dependencies.
Declaration
Swift
public init(networkHelper: NetworkHelperProtocol, resourceCacher: ResourceCacher<Resource>, parameters: RefreshParameters, errorCooldown: ErrorCooldown? = nil, logger: LoggerProtocol?)Parameter Description networkHelperHelper for network operations.
resourceCacherCacher for storing resources locally.
parametersConfiguration parameters for refreshing.
errorCooldownOptional cooldown strategy for handling errors.
loggerOptional logger for debugging.
-
Requests a refresh that is fulfilled if enough time has passed and, optionally, pass a callback to validate the resource and define if it should be accepted and cached.
Only valid resources are reported in the
onResourceLoadedcallback and stored on disk.Declaration
Swift
public func requestRefresh(validatingResource: @escaping (Resource) -> Bool = { _ in true })Parameter Description validatingResourceA callback that passes the refreshed resource as a parameter and needs to return
trueif the resource is valid orfalseif it is not valid. -
Updates the refreshInterval to the specified seconds.
Declaration
Swift
public func setRefreshInterval(_ interval: TimeFrame)Parameter Description secondsThe amount of seconds to wait between refreshes.