Skip to content

Commit a9cb4f4

Browse files
BugFix: Fixing BasemapGallery Sceneview appears blank sometimes. (#660)
* Fixing BasemapGallery Sceneview appears blank sometimes. Refactor NotifySpatialReferenceChanged method calls for improved asynchronous handling. * Using Async Await in foreach block * Introduced a semaphore for thread-safe access to UpdateBasemaps. This avoids multiple invocations of UpdateBasemaps() and NotifySpatialReferenceChanged() * Refactor UpdateBasemaps for better error handling Moved Cancellation operation to get semaphopre release earlier * Modified `UpdateBasemaps` to attempt semaphore entry without waiting. If another update is in progress, the method exits immediately. * Adding missing space * Removing Redundant code * Removing redundant method nesting * Moving logic away from SemaphoreSlim and using pending boolean locks to make sure collate subsequent update calls and make it a single call. * Removed the _isUpdatingBasemaps variable and replaced it with the IsLoading property to manage the loading state. This change simplifies and consolidating it into a single property.
1 parent 709176b commit a9cb4f4

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,34 @@ private async Task HandlePortalChanged()
197197
await UpdateBasemaps();
198198
}
199199

200+
private bool _pendingUpdateBasemaps;
201+
200202
public async Task UpdateBasemaps()
201203
{
204+
_pendingUpdateBasemaps = true;
205+
if (IsLoading)
206+
return;
207+
202208
IsLoading = true;
203-
// Cancel any pending load before starting a new one
204-
_loadCancellationTokenSource?.Cancel();
205-
_loadCancellationTokenSource = new CancellationTokenSource();
206209
try
207210
{
208-
_availableBasemaps = await PopulateBasemaps(_loadCancellationTokenSource.Token);
209-
HandleAvailableBasemapsChanged();
210-
}
211-
catch (Exception ex)
212-
{
213-
System.Diagnostics.Trace.WriteLine(ex.Message);
211+
while (_pendingUpdateBasemaps)
212+
{
213+
_pendingUpdateBasemaps = false;
214+
215+
_loadCancellationTokenSource?.Cancel();
216+
_loadCancellationTokenSource = new CancellationTokenSource();
217+
try
218+
{
219+
_availableBasemaps = await PopulateBasemaps(_loadCancellationTokenSource.Token);
220+
HandleAvailableBasemapsChanged();
221+
}
222+
catch (OperationCanceledException) { }
223+
catch (Exception ex)
224+
{
225+
System.Diagnostics.Trace.WriteLine(ex.Message);
226+
}
227+
}
214228
}
215229
finally
216230
{
@@ -284,18 +298,7 @@ private static bool BasemapIsActuallyNotABasemap(Basemap input)
284298
return await LoadBasemapGalleryItems(Portal, cancellationToken);
285299
}
286300

287-
private Task<ObservableCollection<BasemapGalleryItem>> LoadBasemapGalleryItems(ArcGISPortal portal, CancellationToken cancellationToken = default)
288-
{
289-
if (_loadBasemapGalleryItemsTask is null || _loadBasemapGalleryItemsTask.IsCompleted)
290-
{
291-
_loadBasemapGalleryItemsTask = LoadBasemapGalleryItemsInternal(portal, cancellationToken);
292-
return _loadBasemapGalleryItemsTask;
293-
}
294-
295-
return _loadBasemapGalleryItemsTask;
296-
}
297-
298-
private async Task<ObservableCollection<BasemapGalleryItem>> LoadBasemapGalleryItemsInternal(ArcGISPortal portal, CancellationToken cancellationToken = default)
301+
private async Task<ObservableCollection<BasemapGalleryItem>> LoadBasemapGalleryItems(ArcGISPortal portal, CancellationToken cancellationToken = default)
299302
{
300303
async Task<List<BasemapGalleryItem>> LoadBasemapsAsync(Func<CancellationToken, Task<IEnumerable<Basemap>>> getBasemapsFunc)
301304
{

0 commit comments

Comments
 (0)