diff --git a/src/Core/maps/src/Handlers/Map/MapHandler.Android.cs b/src/Core/maps/src/Handlers/Map/MapHandler.Android.cs index 2426f2a00557..696ba7c46b1c 100644 --- a/src/Core/maps/src/Handlers/Map/MapHandler.Android.cs +++ b/src/Core/maps/src/Handlers/Map/MapHandler.Android.cs @@ -37,6 +37,7 @@ public partial class MapHandler : ViewHandler List? _polygons; List? _circles; Dictionary? _trackedMapElements; + HashSet? _subscribedPins; public GoogleMap? Map { get; private set; } @@ -503,7 +504,8 @@ void AddPins(IList pins) _markers.Add(marker!); } - if (pin is INotifyPropertyChanged observable) + _subscribedPins ??= new HashSet(ReferenceEqualityComparer.Instance); + if (_subscribedPins.Add(pin) && pin is INotifyPropertyChanged observable) { observable.PropertyChanged += PinOnPropertyChanged; } @@ -556,17 +558,18 @@ void PinOnPropertyChanged(object? sender, PropertyChangedEventArgs e) void DisconnectPins() { - if (VirtualView == null) - return; - - for (int i = 0; i < VirtualView.Pins.Count; i++) + if (_subscribedPins is not null) { - var pin = VirtualView.Pins[i]; - if (pin is INotifyPropertyChanged observable) + foreach (var pin in _subscribedPins) { - observable.PropertyChanged -= PinOnPropertyChanged; + if (pin is INotifyPropertyChanged observable) + { + observable.PropertyChanged -= PinOnPropertyChanged; + } + + pin?.Handler?.DisconnectHandler(); } - pin?.Handler?.DisconnectHandler(); + _subscribedPins = null; } }