EF Core, SQLite, multi-threading, concurrency

I built an Xamarin Android application using Entity Framework Core 2 and SQLite.
I create a single context on application start.

It is a transport application where the drivers record arrivals/departures and also package pickup/dropoffs.

I initially designed it with a timer that would upload activities to the the web API every 60 seconds if there were new records, then delete them.
This would occur on the background thread to keep the interface running nice and smooth.
However, I seemed to be getting concurrency errors and I think the time might have been stalling because I wasn't accounting properly for the device going to sleep and waking up.

I moved everything to the main thread, which technically works, but the users complain about the delay when they hit the action buttons because the processing to the API is occuring.

1) How do I send new records to upload to the API in background thread without concurrency issues and in a process that doesn't stall or timeout if the device goes to sleep or loses internet connection?

2) I use Xamarin.Essential to get the GPS location:

var request = new GeolocationRequest(GeolocationAccuracy.Low, TimeSpan.FromSeconds(3));
var location = await Geolocation.GetLocationAsync(request);

From what I understand this can't be run in a background thread. How do I get the GPS location without interfering with the main thread UI? As you can see from my code I am limiting to three seconds to at least make it more palatable to the user.

Sign In or Register to comment.