Both of these work, but the first results in a warning that execution continues after the called to GetDateRanges
and that I should use await
. The call is in a constructor so I don't think I can do that.
Given that the result is the same, is there some other significant difference between these code snippets? Is the first holding up the main thread and I haven't noticed?
public MainPage(IDateRangeRepository dateRangeRepository)
{
GetDateRanges();
}
async Task GetDateRanges()
{
var ranges = await dateRangeRepository.FetchDateRangesAsync(
DateTime.Now, DateTime.Now);
calendar.DateRanges = ranges;
}
With Task.Run...
public MainPage(IDateRangeRepository dateRangeRepository)
{
Task.Run(GetDateRanges);
}
async Task GetDateRanges()
{
var ranges = await dateRangeRepository.FetchDateRangesAsync(
DateTime.Now, DateTime.Now);
Device.BeginInvokeOnMainThread(() =>
{
calendar.DateRanges = ranges;
});
}