OfflineRetrier

@available(macOS 10.14, iOS 12, tvOS 12, watchOS 5, visionOS 1, *)
public final class OfflineRetrier : RequestAdapter, RequestRetrier, RequestInterceptor, Sendable

RequestRetrier which uses NWPathMonitor to detect when connectivity is restored to retry failed requests.

  • Default amount of time to wait for connectivity to be restored before failure. .seconds(5) by default.

    Declaration

    Swift

    public static let defaultWait: DispatchTimeInterval
  • Default Set<URLError.Code> used to check for offline errors. [.notConnectedToInternet] by default.

    Declaration

    Swift

    public static let defaultURLErrorOfflineCodes: Set<URLError.Code>
  • Default method of detecting whether a particular any Error means connectivity is offline.

    Declaration

    Swift

    public static let defaultIsOfflineError: @Sendable (_ error: any Error) -> Bool
  • Creates an instance from the provided NWPathMonitor, maximum wait for connectivity, and offline error predicate.

    Declaration

    Swift

    public init(monitor: @autoclosure @escaping () -> NWPathMonitor = NWPathMonitor(),
                maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
                isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError)

    Parameters

    monitor

    NWPathMonitor() to use to detect connectivity. A new instance is created each time a request fails and retry may be needed.

    maximumWait

    DispatchTimeInterval to wait for connectivity before

    isOfflineError

    Predicate closure used to determine whether a particular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

  • Creates an instance using an NWPathMonitor configured with the provided InterfaceType, maximum wait for connectivity, and offline error predicate.

    Declaration

    Swift

    public convenience init(requiredInterfaceType: NWInterface.InterfaceType,
                            maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
                            isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError)

    Parameters

    monitor

    NWInterface.InterfaceType used to configured the NWPathMonitor each time one is needed.

    maximumWait

    DispatchTimeInterval to wait for connectivity before

    isOfflineError

    Predicate closure used to determine whether a particular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

  • Creates an instance using an NWPathMonitor configured with the provided InterfaceTypes, maximum wait for connectivity, and offline error predicate.

    Declaration

    Swift

    @available(macOS 11, iOS 14, tvOS 14, watchOS 7, visionOS 1, *)
    public convenience init(prohibitedInterfaceTypes: [NWInterface.InterfaceType],
                            maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
                            isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError)

    Parameters

    monitor

    [NWInterface.InterfaceType] used to configured the NWPathMonitor each time one is needed.

    maximumWait

    DispatchTimeInterval to wait for connectivity before

    isOfflineError

    Predicate closure used to determine whether a particular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

  • Declaration

    Swift

    public func retry(_ request: Request,
                      for session: Session,
                      dueTo error: any Error,
                      completion: @escaping @Sendable (RetryResult) -> Void)