Hi. I am new to Moya. Can you please tell me how can I check if there is network connection before I send the request so I can do proper handling like showing user a message that there is no internet connection?
Thanks in advance!
For Reachability you can use NetworkReachabilityManager from Alamofire to listen for the reachability changes and handle the status.
Here is the documentation for using NetworkReachabilityManager. It has best practices listed in the document for networking which you can have look.
Thank you so much! Could you also please tell me what's the best way to cancel requests? I've been looking at different question threads about that but they are all for RxMoya or ReactiveMoya. I am using the usual Moya.
MoyaProvider request method returns a Cancellable protocol type which has a cancel method.
func request(_ target: Target,
callbackQueue: DispatchQueue? = .none,
progress: ProgressBlock? = .none,
completion: @escaping Completion) -> Cancellable
So you return this Cancellable type from your Network Layer method and call cancel when required.
Thank you!
Most helpful comment
For Reachability you can use NetworkReachabilityManager from Alamofire to listen for the reachability changes and handle the status.
Here is the documentation for using NetworkReachabilityManager. It has best practices listed in the document for networking which you can have look.