I have a Xamarin app with multiple tables of data that it pulls from an API. At the moment, the code that calls the API and inserts the data looks like this:
try
{
// Table1
var table1Response = await httpClient.Value.GetStringAsync("http://website.com/api/table1");
var table1 = JsonConvert.DeserializeObject<List<Table1>>(table1Response);
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<Table1>();
conn.DeleteAll<Table1>();
conn.InsertAll(table1);
conn.Close();
}
// Table2
var table2Response = await httpClient.Value.GetStringAsync("http://website.com/api/table2");
var table2 = JsonConvert.DeserializeObject<List<Table2>>(table2Response);
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
// Checks for table and creates if doesn't exist
conn.CreateTable<Table2>();
// Deletes all old data
conn.DeleteAll<Table2>();
// Updates with new reference data from API
conn.InsertAll(table2);
conn.Close();
}
// Repeats for all tables in the app.
Because it has to await the API call, when this method is called in the app's OnStart() the data loads in table by table, and if you navigate to a page in the app that requires certain data that hasn't finished being called yet, obviously the app crashes. I asked yesterday for help with a function I was writing to insert the data programmatically using a foreach loop (see: Xamarin/C# - Insert data into SQLite database programmatically for multiple tables?) and I was informed that that's not a smart way to do it and defining each table as you need it is a better method.
My question now becomes, what can I do to my API calling method to have it make multiple API calls at once and hypothetically load all the data in one shot? Is this even possible to do, or should I try building a Loading screen of sorts that waits until all the data has been retrieved before allowing further use of the app?