ResourceRefresher

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.

  • id

    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?)

    Parameters

    networkHelper

    Helper for network operations.

    resourceCacher

    Cacher for storing resources locally.

    parameters

    Configuration parameters for refreshing.

    errorCooldown

    Optional cooldown strategy for handling errors.

    logger

    Optional 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 onResourceLoaded callback and stored on disk.

    Declaration

    Swift

    public func requestRefresh(validatingResource: @escaping (Resource) -> Bool = { _ in true })

    Parameters

    validatingResource

    A callback that passes the refreshed resource as a parameter and needs to return true if the resource is valid or false if it is not valid.

  • Updates the refreshInterval to the specified seconds.

    Declaration

    Swift

    public func setRefreshInterval(_ interval: TimeFrame)

    Parameters

    seconds

    The amount of seconds to wait between refreshes.