I've tested various backgrounding options (xamarin samples, xamarin blog articles, various sources) but I'd like some clarification on the pro/cons of each one, and if some are deprecated/obsolete.
My case is an app (wifi, on client premises) that should periodically poll a server for new data, or push collected data to the server as soon as it has connection (it should continue to collect data if no connection is available, but push to the server as soon as it has connection).
I want this sync to be in the background for the user, who in the meantime can continue to work. I would like the push (if data is present) to be done each 1 or 2 minutes, so I can just create a task in the background job with an infinite while that checks every 1/2 minutes.
I made some basic samples to test:
1) using a LongRunningTaskService : Service
2) using Firebase.JobDispatcher
3) using WorkManager (but scheduled jobs can't be less than 15 minutes)
4) looking at Shiny, but currently having trouble integrating with Prism (but I guess I'll make it work)(but don't understand if it is a wrapper of what exactly?)
Which solution do you think is appropriate for my use case? With all the 4 solutions, data should be pushed when the app is in foreground or background (right?)
In case I need to push data only when the app is in the foreground, would it be wrong to start my Task in App class?
UPDATE
Tried this in the OnInitialized() of the App.cs:
Task.Factory.StartNew(async () =>
{
while (true)
{
await BackgroundTasks.TestPushDataRepeat();
await Task.Delay(60000);
}
}
, TaskCreationOptions.LongRunning);
It works every minute when the app is in foreground (and doesn't block the UI), but unexpectedly works also when the app is backgrounded (that is a not needed plus, for my case). Not being a service, I thought it should have freezed, why is that?
I'm trying to better understand/separate how the TPL works with Xamarin on Android, and how Android backgrounding (services/workers etc) works, to see if this solution has drawbacks.