Is there any recommendation on using Alamofire's RequestRetrier to keep retrying a request until it succeeds?
The issue I'm running into is every time a retry happens it increases the memory of the app. I have a scenario that a user might make a request offline and it should eventually succeed when they regain connection.
Is it not good practice to continually retry a request? Should I use a Timer instead to to recreate the network call if it fails a few times? Should I use some other mechanism that waits for network activity and then sends up a queue of requests (any suggestions on what that would be would be appreciated!)?
Here is an example to see the memory explode. You just need to put your iPhone in airplane mode (so there is no connection) and the memory will keep going up rapidly since the call will happen a lot in this case.
import UIKit
import Alamofire
class ViewController: UIViewController {
let session = Session(configuration: .default)
override func viewDidLoad() {
super.viewDidLoad()
sendRequest()
}
func sendRequest() {
session.request("https://www.google.com", method: .get, interceptor: RetryHandler())
.responseData { (responseData) in
switch responseData.result {
case .success(let data):
print(String(data: data, encoding: .utf8) ?? "nope")
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
class RetryHandler: RequestInterceptor {
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
// keep retrying on failure
completion(.retry)
}
}(