From 6f1fb78c69fb06e1017fa738c1e0a013c981c693 Mon Sep 17 00:00:00 2001 From: dankinsoid <30962149+dankinsoid@users.noreply.github.com> Date: Sun, 24 Mar 2024 16:59:34 +0300 Subject: [PATCH] 0.47.0 --- README.md | 2 +- .../Modifiers/BackgroundModifiers.swift | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b33af2a..bd0aa70 100644 --- a/README.md +++ b/README.md @@ -257,7 +257,7 @@ import PackageDescription let package = Package( name: "SomeProject", dependencies: [ - .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.46.0") + .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.47.0") ], targets: [ .target( diff --git a/Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift b/Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift index 63ffce9..91b9bdd 100644 --- a/Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift +++ b/Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift @@ -47,19 +47,23 @@ private struct RetryOnEnterForegroundMiddleware: HTTPClientMiddleware { configs: APIClient.Configs, next: (URLRequest, APIClient.Configs) async throws -> (T, HTTPURLResponse) ) async throws -> (T, HTTPURLResponse) { - let wasInBackground = WasInBackgroundService() - let isInBackground = await UIApplication.shared.applicationState == .background - if !isInBackground { - await wasInBackground.start() - } - do { - return try await next(request, configs) - } catch { - if await wasInBackground.wasInBackground { + func makeRequest() async throws -> (T, HTTPURLResponse) { + let wasInBackground = WasInBackgroundService() + var isInBackground = await UIApplication.shared.applicationState == .background + if !isInBackground { + await wasInBackground.start() + } + do { return try await next(request, configs) + } catch { + isInBackground = await UIApplication.shared.applicationState == .background + if !isInBackground, await wasInBackground.wasInBackground { + return try await makeRequest() + } + throw error } - throw error } + return try await makeRequest() } }