diff --git a/src/Couchbase.Lite.Support.Apple/iOS/Support/iOSReachability.cs b/src/Couchbase.Lite.Support.Apple/iOS/Support/iOSReachability.cs index 9add27b39..8133a27bb 100644 --- a/src/Couchbase.Lite.Support.Apple/iOS/Support/iOSReachability.cs +++ b/src/Couchbase.Lite.Support.Apple/iOS/Support/iOSReachability.cs @@ -43,7 +43,7 @@ internal sealed class iOSReachability : IReachability private NetworkReachability _ref; private DispatchQueue _queue; - private AtomicBool _started; + private bool _started; public event EventHandler StatusChanged; @@ -102,42 +102,51 @@ private void NotifyFlagsChanged(NetworkReachabilityFlags flags) public void Start() { - if (_started.Set(true)) + _queue.DispatchSync(() => { - return; - } - - if (Url == null) - { - _ref = new NetworkReachability(new IPAddress(0)); - } - else - { - _ref = new NetworkReachability(Url.Host); - } - - _ref.SetDispatchQueue(_queue); - _ref.SetNotification(ClientCallback); - if (_ref.GetFlags(out var flags) == StatusCode.OK) - { - Log.To.Sync.I(Tag, $"{this}: flags={flags}; starting..."); - NotifyFlagsChanged(flags); - } - else - { - Log.To.Sync.I(Tag, $"{this}: starting..."); - } + if (_started) { + return; + } + + _started = true; + + if (String.IsNullOrEmpty(Url?.Host)) + { + _ref = new NetworkReachability(new IPAddress(0)); + } + else + { + _ref = new NetworkReachability(Url.Host); + } + + _ref.SetDispatchQueue(_queue); + _ref.SetNotification(ClientCallback); + if (_ref.GetFlags(out var flags) == StatusCode.OK) + { + Log.To.Sync.I(Tag, $"{this}: flags={flags}; starting..."); + NotifyFlagsChanged(flags); + } + else + { + Log.To.Sync.I(Tag, $"{this}: starting..."); + } + }); } public void Stop() { - if (!_started.Set(false)) + _queue.DispatchSync(() => { - return; - } - - ReachabilityKnown = false; - _ref.SetDispatchQueue(null); + if (!_started) { + return; + } + + _started = false; + ReachabilityKnown = false; + _ref?.SetDispatchQueue(null); + _ref?.Dispose(); + _ref = null; + }); } #endregion