Forum Xamarin.Forms

Confusion between all backgrounding options (and their evolution in time) with Xamarin.Forms Android

MattiaDurliMattiaDurli ITMember ✭✭

I've made tests with various backgrounding options 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 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 attempt (if data is present) to be done every 1 or 2 minutes.

I made some basic samples to test:

1) using a LongRunningTaskService : Service
2) using Firebase.JobDispatcher (deprecated in favor of WorkManager right?)
3) using WorkManager (but scheduled jobs can't be less than 15 minutes)
4) using Shiny, but currently having trouble integrating with Prism (but I guess I'll make it work) (also, Shiny is a wrapper of WorkManager, right?)

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?)
My priority is the push attempt to be done every minute (if data has been collected), even if only when the app is in foreground.
If I use WorkManager/Shiny, and schedule a job to repeat I see that it continues to run even when my app is closed, but the minimum period is 15 minutes. But then, for how long it does that, after I close the app? when will it be definitely killed?

In case I need to push data only when the app is in the foreground, would it be wrong to start a fore and forget Task in App class with an infinite loop that pushes every minute? or something like:

Device.StartTimer(new TimeSpan(0, 0, 60), () =>
// push data if present
Debug.WriteLine("Repeating" + DateTime.Now);
return true;

Recap: I need to try to push data every 1 minute in the background when the app is in the foreground, possibly do the same when in the background, and try for a while when is closed, even every 15 minutes.
Which path should I follow?

Thanks, Mattia

Sign In or Register to comment.