Request Retrying when offline #3948
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue Link 🔗
No specific issue was raised for this feature; this PR introduces a network-aware request retrier that automatically reattempts failed requests when the network becomes available.
Goals ⚽
Implement an automatic retry mechanism that waits for network availability.
Improve request handling by reducing manual retry efforts for network-related failures.
Implementation Details 🚧
Introduced NetworkAwareRetrier, a custom RequestRetrier that integrates with Alamofire sessions.
Utilized NWPathMonitor to detect network status changes and store pending retries.
Ensured thread safety using a concurrent queue with barrier flags for state management.
Maintained a queue of pending retry completions, executing them once the network is restored.
Usage 🚀
1️⃣ Adding NetworkAwareRetrier to an Alamofire Session
To enable automatic request retries for all requests within a session:
2️⃣ Using NetworkAwareRetrier for a Specific Request
If you want to apply the retrier only to a specific request without affecting the entire session, you can use
request(_:method:interceptor:):
This ensures that only the specific request will retry when network connectivity is restored, while other requests in the session remain unaffected.
Testing Details 🔍
Unit Tests: Verified that failed requests are queued and retried once the network becomes available.
Manual Testing: Simulated network loss and recovery scenarios to confirm expected retry behavior.
Concurrency Testing: Ensured thread-safe access to shared properties.