Backendless Support
 
Answered

Is it good practice to check internet connection before each api call (register,save object..etc) ?

Hi,

as the question above says, Is it good practice to check internet connection before each api call (register, save object, get object, update geo ... etc) in my app ????

using swift code like :

  1. import SystemConfiguration

    public class Reachability {

    class func isConnectedToNetwork() -> Bool {

    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))

    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))

    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {

    $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in

    SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)

    }

    }

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)

    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {

    return false

    }

    /* Only Working for WIFI

    let isReachable = flags == .reachable

    let needsConnection = flags == .connectionRequired

    return isReachable && !needsConnection

    */

    // Working for Cellular and WIFI

    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0

    let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

    let ret = (isReachable && !needsConnection)

    return ret

    }

    }

and then check anywhere in the app:

  1. if Reachability.isConnectedToNetwork() == true

    {

    print("Internet Connection Available!")

    }

    else

    {

    print("Internet Connection not Available!")

    }

OR stick with the error code throw back from each api call ?

Best Answer
photo

Hello,

Rather than checking internet connection before every API call, it would be better to create a global callback in your app so it is notified when the connection is dropped. Then, it would be easier for you to check if the connection exists simply by referencing a global variable.

Alternatively, you should be adding fault handling for all of your calls anyway. If the connection is dropped, a fault will be generated and you could handle it in a user-friendly way.

Regards,

Mark

Leave a Comment

Comments (4)

photo
1

Hello,

Rather than checking internet connection before every API call, it would be better to create a global callback in your app so it is notified when the connection is dropped. Then, it would be easier for you to check if the connection exists simply by referencing a global variable.

Alternatively, you should be adding fault handling for all of your calls anyway. If the connection is dropped, a fault will be generated and you could handle it in a user-friendly way.

Regards,

Mark

photo
1

Hi Mark,

thanks for the quick respond .. so you're saying the best option is to handle fault for all calls as the example below :

  1. Types.tryblock({ () -> Void in
  2. // sychronous backendless API call here
  3. },
  4. catchblock: { (exception) -> Void in
  5. // handle error from server here
  6. if ((exception as! Fault).faultCode == "-1009") {

    print ("The Internet connection appears to be offline")

    }

    })

??????

photo
1

If you're developing with Swift, yes, that's how you catch the reported errors.

It is a good idea to have fault handling not just for missing internet connection, but all other cases as well.

Regards,

Mark

photo
1

Thanks for the help Mark :)