Moya: How to check if there is internet connection?

Created on 26 Oct 2018  路  4Comments  路  Source: Moya/Moya

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!

question

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.

All 4 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

PlutusCat picture PlutusCat  路  3Comments

hjzCy picture hjzCy  路  3Comments

kamwysoc picture kamwysoc  路  3Comments

Tynox picture Tynox  路  3Comments