I would like to use the Android Worker to perform periodic downloads of data from my API. This is work that will run in the background, it will not be invoked from a user action, and I don't need to feed back the result to the UI.
My question is - I know that the Worker is intended to run synchronous code, so in this specific instance is it ok if I invoke my async method in a synchronous way? (BTW - I don't think a Listenable Worker is what I'm after since it is invoked from the Main Thread).
I know 99% of the time calling an async method in a synchronous way is not advisable, but in this case it's (1) on a background thread (2) requires no feedback to the user, even if there is an error, and (3) writing synchronous methods is a significant near duplication of a lot of async code that I have already written (for instances where the user presses a button to perform a manual sync).
To make my sync method sychronous I was going to wait for the Result of the SyncAsync() method as below.
i.e. change var response = await SyncManager.SyncAsync();
to var response = SyncManager.SyncAsync().Result;
Is this acceptable in this instance?
public class SyncWorker : Worker
{
public SyncWorker(Context context, WorkerParameters workerParams) : base(context, workerParams)
{
}
public override Result DoWork()
{
var response = SyncManager.SyncAsync().Result;
return response.IsSuccess ? new Result.Success() : new Result.Failure();
}
}