0

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?

Nick
  • 155
  • 1
  • 10

1 Answers1

0

You can try to use backgrounding Service in xamarin form.

Background Services are very commonly used for tasks that are performed in the background, such as downloading files, long calculations, playing music, and periodic method call. Each platform has a different way on enabling and registering the Background Service.

Because each platform has their own way and rules for background services. We have to make services for each platform.

For more details, you can check:

https://www.youtube.com/watch?v=Z1YzyreS4-o

https://robgibbens.com/backgrounding-with-xamarin-forms/

https://winstongubantes.blogspot.com/2018/09/backgrounding-with-xamarinforms-easy-way.html

Besides, if you want to dig deeper about advance Background Service per each platform you can check on this links below:

Android BackgroundServices LINK

iOS BackgroundServices LINK

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19