Move the background task logic into ShowsService, and only do it on first load.

pull/57/head
Eric Erhardt 2022-03-30 19:03:11 -05:00
parent 4ae65e91df
commit 3efa4b3579
2 changed files with 21 additions and 4 deletions

View File

@ -9,6 +9,7 @@ public class ShowsService
{
private readonly HttpClient httpClient;
private readonly ListenLaterService listenLaterService;
private bool firstLoad = true;
public ShowsService(ListenLaterService listenLaterService)
{
@ -73,7 +74,25 @@ public class ShowsService
return new Show(response, listenLaterService);
}
private async Task<T> TryGetAsync<T>(string path)
private Task<T> TryGetAsync<T>(string path)
{
if (firstLoad)
{
firstLoad = false;
// On first load, it takes a significant amount of time to initialize
// the ShowsService. For example, Connectivity.NetworkAccess, Barrel.Current.Get,
// and HttpClient all take time to initialize.
//
// Don't block the UI thread while doing this initialization, so the app starts faster.
// Instead, run the first TryGet in a background thread to unblock the UI during startup.
return Task.Run(() => TryGetImplementationAsync<T>(path));
}
return TryGetImplementationAsync<T>(path);
}
private async Task<T> TryGetImplementationAsync<T>(string path)
{
var json = string.Empty;

View File

@ -49,9 +49,7 @@ public class DiscoverViewModel : BaseViewModel
private async Task FetchAsync()
{
// run GetShowsAsync in the background, so things get initialized in the background
// like checking the cache and the web request
var podcastsModels = await Task.Run(() => showsService.GetShowsAsync());
var podcastsModels = await showsService.GetShowsAsync();
if (podcastsModels == null)
{