I have followed this blog by Stephen Cleary and I was wondering what would be the best approach for update the existing collection that is bounded to the UI which updates every 15 seconds? For e.g. do I Clear the list then add a new collection to create a new object?
I am asking this question because when I added this Task taskA = Task.Run(() => UpdateManifest(_ManifestToken.Token));
line of code my CPU increase rapidly.
C#:
// Ctor.
public ManifestViewModel()
{
_ManifestItems = new NotifyTaskCompletion<ObservableCollection<ManifestItem>>(FetchData());
Task taskA = Task.Run(() => UpdateManifest(_ManifestToken.Token));
}
private NotifyTaskCompletion<ObservableCollection<ManifestItem>> _ManifestItems;
public NotifyTaskCompletion<ObservableCollection<ManifestItem>> ManifestItems
{
get => _ManifestItems;
set
{
if (_ManifestItems != value)
{
_ManifestItems = value;
OnPropertyChanged();
}
}
}
public static Task UpdateManifest(CancellationToken token)
{
while (true)
{
_ManifestItems = new NotifyTaskCompletion<ObservableCollection<ManifestItem>>(FetchData());
Task.Delay(15000);
}
}