I have Xamarin.Forms project implementing DependencyService with platform specific code for Android, iOS and UWP. I created interface for DependencyService with couple asynchronous methods. For some platforms platform specific code is asynchronous and for others synchronous.
Let's say I have an interface in Xamarin.Forms project:
public interface IMyInterface
{
Task FooAsync();
}
Android implementation of IMyInteface:
public class MyAndroidImplementation : IMyInterface
{
public async Task FooAsync()
{
await DoSomethingAsync();
}
}
iOS implementation of IMyInterface:
public class MyiOSImplementation : IMyInterface
{
public async Task FooAsync()
{
DoSomething();
}
}
There is only synchronous code in iOS implementation. This raises warning with message:
This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
What is the proper way of dealing with this type of scenario?
I don't like wrapping synchronous code to asynchronous 'await Task.Run()' since this is clearly a lie that there is an asynchronous code. Would it be better to design IMyInterface with void method and then in platform specific implementation run the code on 'Task.Run()'?