generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a000c6e
commit 9633d73
Showing
11 changed files
with
293 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
public extension APIClient { | ||
|
||
/// Execute the http request in the background task. | ||
/// | ||
/// To know more about background task, see [Apple Documentation](https://developer.apple.com/documentation/backgroundtasks) | ||
func backgroundTask() -> Self { | ||
httpClientMiddleware(BackgroundTaskMiddleware()) | ||
} | ||
|
||
/// Retry the http request when enter foreground if it was failed in the background. | ||
func retryIfFailedInBackground() -> Self { | ||
httpClientMiddleware(RetryOnEnterForegroundMiddleware()) | ||
} | ||
} | ||
|
||
private struct BackgroundTaskMiddleware: HTTPClientMiddleware { | ||
|
||
func execute<T>( | ||
request: URLRequest, | ||
configs: APIClient.Configs, | ||
next: (URLRequest, APIClient.Configs) async throws -> (T, HTTPURLResponse) | ||
) async throws -> (T, HTTPURLResponse) { | ||
let id = await UIApplication.shared.beginBackgroundTask( | ||
withName: "Background Task for \(request.url?.absoluteString ?? "")" | ||
) | ||
guard id != .invalid else { | ||
return try await next(request, configs) | ||
} | ||
do { | ||
let result = try await next(request, configs) | ||
await UIApplication.shared.endBackgroundTask(id) | ||
return result | ||
} catch { | ||
await UIApplication.shared.endBackgroundTask(id) | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
private struct RetryOnEnterForegroundMiddleware: HTTPClientMiddleware { | ||
|
||
func execute<T>( | ||
request: URLRequest, | ||
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 { | ||
return try await next(request, configs) | ||
} | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
private final actor WasInBackgroundService { | ||
|
||
public private(set) var wasInBackground = false | ||
private var observer: NSObjectProtocol? | ||
|
||
public func start() async { | ||
observer = await NotificationCenter.default.addObserver( | ||
forName: UIApplication.didEnterBackgroundNotification, | ||
object: nil, | ||
queue: nil | ||
) { [weak self] _ in | ||
guard let self else { return } | ||
Task { | ||
await self.setTrue() | ||
} | ||
} | ||
} | ||
|
||
public func reset() { | ||
wasInBackground = false | ||
} | ||
|
||
private func setTrue() { | ||
wasInBackground = true | ||
} | ||
} | ||
#endif |
Oops, something went wrong.