Skip to content

Commit 81a8009

Browse files
committed
Fix issue with mumtiple requests comming to the incremental loading collection
1 parent 3d6b98f commit 81a8009

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Microsoft.Toolkit.Uwp/IncrementalLoadingCollection/IncrementalLoadingCollection.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class IncrementalLoadingCollection<TSource, IType> : ObservableCollection
3030
ISupportIncrementalLoading
3131
where TSource : Collections.IIncrementalSource<IType>
3232
{
33+
private static readonly SemaphoreSlim _mutex = new SemaphoreSlim(1);
34+
3335
/// <summary>
3436
/// Gets or sets an <see cref="Action"/> that is called when a retrieval operation begins.
3537
/// </summary>
@@ -226,19 +228,29 @@ public Task RefreshAsync()
226228
/// </returns>
227229
protected virtual async Task<IEnumerable<IType>> LoadDataAsync(CancellationToken cancellationToken)
228230
{
229-
var result = await Source.GetPagedItemsAsync(CurrentPageIndex, ItemsPerPage, cancellationToken)
230-
.ContinueWith(
231-
t =>
232-
{
233-
if (t.Status == TaskStatus.RanToCompletion)
231+
// TODO (2021.05.05): Make use common AsyncMutex class.
232+
// AsyncMutex is located at Microsoft.Toolkit.Uwp.UI.Media/Extensions/System.Threading.Tasks/AsyncMutex.cs at the time of this note.
233+
await _mutex.WaitAsync();
234+
try
235+
{
236+
var result = await Source.GetPagedItemsAsync(CurrentPageIndex, ItemsPerPage, cancellationToken)
237+
.ContinueWith(
238+
t =>
234239
{
235-
CurrentPageIndex += 1;
236-
}
240+
if (t.Status == TaskStatus.RanToCompletion)
241+
{
242+
CurrentPageIndex += 1;
243+
}
237244

238-
return t.Result;
239-
}, cancellationToken);
245+
return t.Result;
246+
}, cancellationToken);
240247

241-
return result;
248+
return result;
249+
}
250+
finally
251+
{
252+
_mutex.Release();
253+
}
242254
}
243255

244256
private async Task<LoadMoreItemsResult> LoadMoreItemsAsync(uint count, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)