From 060c7294f4c5091220a13b168e75424591cc07e9 Mon Sep 17 00:00:00 2001 From: Morten Nielsen Date: Tue, 25 Jul 2023 15:32:10 -0700 Subject: [PATCH] Avoid race-condition where layers are loading and populating as map starts up. Can cause `InvalidOperationException: Collection was modified; enumeration operation may not execute.` while map is loading. Also listen to collection changed events first, to ensure we're always up to date. --- src/Toolkit/Toolkit/LayerContentDataSource.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Toolkit/Toolkit/LayerContentDataSource.cs b/src/Toolkit/Toolkit/LayerContentDataSource.cs index 326ad1f47..fc866c9fb 100644 --- a/src/Toolkit/Toolkit/LayerContentDataSource.cs +++ b/src/Toolkit/Toolkit/LayerContentDataSource.cs @@ -362,7 +362,18 @@ private IEnumerable TrackLayerContentsRecursive(IEnumerable, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, incc) + { + OnEventAction = static (instance, source, eventArgs) => instance.Layers_CollectionChanged(source, eventArgs), + OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent, + }; + incc.CollectionChanged += listener.OnEvent; + yield return listener.Detach; + } + + foreach (var layer in layers.ToArray()) { if (layer is INotifyPropertyChanged inpc) { @@ -375,22 +386,11 @@ private IEnumerable TrackLayerContentsRecursive(IEnumerable, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, incc) - { - OnEventAction = static (instance, source, eventArgs) => instance.Layers_CollectionChanged(source, eventArgs), - OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent, - }; - incc.CollectionChanged += listener.OnEvent; - yield return listener.Detach; - } } }