8000 Request Retrying when offline by gouravkmar · Pull Request #3948 · Alamofire/Alamofire · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Request Retrying when offline #3948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gouravkmar
Copy link
@gouravkmar gouravkmar commented Mar 24, 2025

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:

let retrier = NetworkAwareRetrier()
let session = Session(interceptor: retrier)

session.request("https://example.com/api/").response { response in
    print("Request completed")
}

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:):

let retrier = NetworkAwareRetrier()
let session = Session()

session.request("https://example.com/api/", interceptor: retrier).response { response in
    print("Request completed")
}

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.

@jshier
Copy link
Contributor
jshier commented Mar 25, 2025

Thanks for the PR, we've wanted something like this! However, there's no need for custom caching here, Alamofire's retry feature already handles that. All you need to do here is create a custom RequestRetrier-conforming type and handle the retry logic in there, Alamofire can asynchronously wait to know whether to retry.

Additionally, given the imminent deprecation of NetworkReachabilityManager, you should rebuild on top of NWPathMonitor. That will ensure it works well into the future.

With those changes I can do a more detailed review and we can get this merged.

@gouravkmar
Copy link
Author
gouravkmar commented Mar 26, 2025

Thanks for the PR, we've wanted something like this! However, there's no need for custom caching here, Alamofire's retry feature already handles that. All you need to do here is create a custom RequestRetrier-conforming type and handle the retry logic in there, Alamofire can asynchronously wait to know whether to retry.

Additionally, given the imminent deprecation of NetworkReachabilityManager, you should rebuild on top of NWPathMonitor. That will ensure it works well into the future.

With those changes I can do a more detailed review and we can get this merged.

I've added the support of multiple failed requests in case of session wide retrier and removed the custom caching flow, please do check @jshier

@gouravkmar gouravkmar changed the title Request caching when offline Request Retrying when offline Apr 1, 2025
@gouravkmar
Copy link
Author

@marcprux can you please help merge this in case this is fine?

Copy link
@monicadiaz68071978 monicadiaz68071978 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0