Search code examples
iosnetwork-programmingalamofire

By industry practices, should I check internet reachability before making API call or during API call in Alamofire


I just started doing networking through Alamofire, and I am a newbie when it comes to Networking

I am working on response validations in Alamofire Api call, So By industry practices Is it better to check the internet reachability before api request or handling it by the error code that alamofire receives!

I've read that checking internet connection before making internet call causes application overhead

{
   let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 60      
        manager.request("http://api.androidhive.info/contacts/")
          .validate()
          .responseJSON {
            response in
            switch (response.result) {
            case .success:
               //  code

                }

            case .failure(let error):
                if error._code == NSURLErrorTimedOut {
                    print("Request call timed Out !!")
                    self.displayText?.text = "Request call timed Out !!"

                } else if error._code == -1009{
                    print(" You Sir are not Connected to the Internet !!")
                    self.displayText?.text = "You Sir are not Connected to the Internet !!"

                } else if error._code == -1003 {
                    print(" \t bruh, \n Atleast provide a Valid URL ")
                    self.displayText?.text = "bruh,Atleast provide a Valid URL "
                }
                else {
                    print("Meh, Some Kind of error with errorCode: \(error._code) !!")
                    self.displayText?.text = "Meh, Some Kind of error with errorCode: \(error._code) !!"
               }

            }

        }

Solution

  • No, you should not check for internet connectivity before making a call. Apple specifically recommended against this. Reachability should only be used after a request failure in case you want to automatically retry requests when the connection is back online.