From 1ac4bbd799d91502460a75d16bcdb66b2c08e6b7 Mon Sep 17 00:00:00 2001 From: Eser Kucuker Date: Thu, 31 Jul 2025 13:39:19 +0300 Subject: [PATCH] Add support for waitsForConnectivity in session config Introduces a method to configure URLSession's waitsForConnectivity property via NetworkableConfigs. This allows requests to wait for network connectivity before proceeding, improving reliability in poor network conditions. --- .../Config/NetworkableConfigs.swift | 7 +++++++ Sources/MBAsyncNetworking/Config/Session.swift | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Sources/MBAsyncNetworking/Config/NetworkableConfigs.swift b/Sources/MBAsyncNetworking/Config/NetworkableConfigs.swift index 062b976..b1c4a3f 100644 --- a/Sources/MBAsyncNetworking/Config/NetworkableConfigs.swift +++ b/Sources/MBAsyncNetworking/Config/NetworkableConfigs.swift @@ -52,4 +52,11 @@ public enum NetworkableConfigs { public func set(configuration: URLSessionConfiguration) { Session.shared.configuration = configuration } + + /// Configures the URLSession to wait for network connectivity before attempting requests. + /// When enabled, requests will wait for an appropriate network connection to become available. + /// - Parameter shouldWait: `true` to wait for connectivity, `false` to fail immediately if no network is available. + public func setConnectivityWaiting(_ shouldWait: Bool) { + Session.shared.setConnectivityWaiting(shouldWait) + } } diff --git a/Sources/MBAsyncNetworking/Config/Session.swift b/Sources/MBAsyncNetworking/Config/Session.swift index f0478cd..e2e47b9 100644 --- a/Sources/MBAsyncNetworking/Config/Session.swift +++ b/Sources/MBAsyncNetworking/Config/Session.swift @@ -108,6 +108,18 @@ public final class Session { ) } + /// Sets the connectivity waiting behavior for the URLSession configuration. + /// This recreates the URLSession with the updated configuration. + /// - Parameter shouldWait: Whether the session should wait for connectivity before making requests. + func setConnectivityWaiting(_ shouldWait: Bool) { + configuration.waitsForConnectivity = shouldWait + session = URLSession( + configuration: configuration, + delegate: delegate, + delegateQueue: nil + ) + } + /// Structure to define timeout configurations for URLSession struct TimeOut { /// The timeout interval when waiting for additional data