I have this async function:
private async Task<bool> ShowAboutToMatchAlert()
{
bool res = true;
if(Preferences.Get("WantSeeFirstMatchAlertAgain", true))
{
Device.BeginInvokeOnMainThread(async () =>
{
bool answer = await DisplayAlert("...");
if (answer)
{
Preferences.Set("WantSeeFirstMatchAlertAgain", false);
res = true;
}
else
{
res = false;
}
});
}
return res;
}
What happens is this functions shows an alert. As soon as the user picks an option (yes or no) the if(answer)
is fired and depending on the input, the result is given.
However, the whole function does not wait for the result and immediately skips forward to the final return. So always true
is being returned (as the function is async).
The function itself has to be async in order for it to be awaited since this function is also called from another async function.
How can I make this function await
the if
part and not just skip forward?
Simplest solution of course would be to make this function be not async but then I cannot return "true" or "false" anymore but have to return System.Threading.Task.Task<bool>
. So that is not an option.