Our project needs to know if it’s got a connection to Realm (Flexible Sync) or not. There are some tasks that should not happen if the app is offline (deleting a certain object for example).
We are using KVO to determine connection status - however, when the app goes from connected to disconnected it can sometimes take up a minute or longer for the event to fire (the opposite happens almost instantly)
Any suggestions on how to know if the app has gone offline/disconnected faster?
Here’s the code we’re using for tracking connection status. self.observer
is a NSKeyValueObservation
func showConnectionStatus(realm: Realm) {
let session = realm.syncSession
// Observe connectionState for changes using KVO
self.observer = session!.observe(\.connectionState, options: [.initial]) { (syncSession, change) in
switch syncSession.connectionState {
case .connecting:
print(" -> Connecting...")
case .connected:
print(" -> Connected")
case .disconnected:
print(" -> Disconnected")]
default:
break
}
}
}