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 :
- 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:
- 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 ?
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
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
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 :
print ("The Internet connection appears to be offline")
}
})
??????
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
Thanks for the help Mark :)
Leave a Comment
Comments have been locked on this page!