Search code examples
multithreadingxamarin.formsasync-awaitentity-framework-coreef-bulkinsert

Xamarin Froms Bulk data insertion hangs application


I have added data sync in my application. When the application is installed, it prompts the user to sync data. During my testing, i had limited amount of records and it was working fine. Now that I have attached the real database, the app hangs and becomes unresponsive during sync. An alert to close the app is also shown on android device.

For syncing, I am doing this

bool response1 = await syncCustomers();
bool response2 = await syncItems();
if(response1 and response2)
{
     do something
}

Both syncCustomers() and syncItems() are tasks which return bool. They fetch the data from API, clear the existing database tables and populate the table with the newly fetched data.

During the syncing process, an activity indicator is shown. When the dataset is small, the app remains responsive and indicator keeps on spinning until the data is synced (which is not long). But with a large dataset, the app hangs and needs to be closed. There are about 6 to 7 thousand records in the live database.

When I start the app next time, the items and customers exist in the database. Which means that the data is added properly. But for some reason, the app hangs.


Solution

  • I came up with a solution. I was calling the above given snippet directly which I believe was running in the UI thread. Now I am running the code in

    Task.Run(some code);
    

    and it runs perfectly. The activity indicator keeps on spinning and the app does not hang.