I have a view-model which is a UDP network browser for my Android and iOS app (fueled by Xamarin). UDP is used to broadcast the active instance of the application and to listen to broadcasts in order to discover other app instances on the local (Wifi) network.
My view-model has property
public AppInstanceList AppInstances
{
get; set;
}
And there is a method
public async Task StartDiscoveringAppInstancesAsync()
which sets up a while loop which keeps on listening via UPD asynchronously:
while(this.networkService != null)
{
var discoveredInstance = await this.networkService.DiscoverAppInstanceAsync ();
// Omitted: store the discovered instance and add to list of discovered instances.
this.AppInstances.Add(...);
// Wait a bit before checking again.
await Task.Delay(1000);
}
My problem with this method is the return type of Task
. Obviously, the method should not be awaited - it is as endless loop and runs until the discovery is stopped.
So should I change it to be void
? But this would violate async/await principles where void
should only be used for events.
Whenever I encounter a topic like that I'm wondering if my implementation is actually bad design and there's a different and cleaner approach. Or is the user of the view-model just not supposed to await
the outcome of the method?