-1

I have this method:

    private async Task BackArrowTappedHandler()
    {
        Task<bool> sp = SavePageDataAsync();
        await Task.WhenAll(sp);
        var saveResult = sp.Result; 

        if (!saveResult)
            return;
        if (Device.RuntimePlatform is Device.iOS)
            await Navigation.PopModalAsync();
        else
            await Shell.Current.Navigation.PopAsync();
    }

Which calls a method that I may or may not want to override:

    public virtual async Task<bool> SavePageDataAsync()
    {
        return true;
    }

Here is a short example of how I was doing the override just to make it more clear:

    public async Task<bool> OnSavePageAsync()
    {
        var IsValueLess = false;
        if (daily < 20)
        {
            msg1 = "Minimum words";
            msg2 = "Please set a number greater than 20";
            IsValueLess = true;
        }
       
        var alertDeckPopup = new AlertPopup(msg2);
        await PopupNavigation.Instance.PushAsync(alertDeckPopup);

        if (IsValueLess)
            return false;
        else
            return true;
    }

What I want to do is to check the result of SavePageDataAsync and based on the result, return or continue.

The only idea I have so far is to use Task.WhenAll

Can someone tell me if there is a better way that I can do this or if this is the only way then is it a valid way to do the check?

This code is giving me an error as I do not know how to correctly call SavePageData and then check the return value. Can someone give me advice on how I can do this?

  • SavePageDataAsync does not use `await` and therefore does not need to be marked `async`. Omit async and return `Task.FromResult(true)` instead, or better, mark it as abstract so that inheritors have to provide an implementation that actually does what it says on the tin – Caius Jard Apr 10 '21 at 07:53
  • What I was doing was making the method when it overrides use Async. I will add in an example of that method also. Can you have a look at the way I am doing the override and let me know if you think that it is still not being done correctly? Note that if possible there are times when there is no need to save data so I don't want to override the method, instead I would just like the method to return true. – Richard Scholey Apr 10 '21 at 07:54

1 Answers1

1

You can use the await operator on the async method to wait for the result of the sub methid

bool sp = await SavePageDataAsync();
if(sp) 
{
// do action if true
} 
Tobias Thieron
  • 281
  • 1
  • 9
  • Caius is suggesting that I am not doing the check correctly. I added in more information to the question. Can you have a look and give me your opinion. – Richard Scholey Apr 10 '21 at 07:59
  • As I see, your new implementation of the `OnSavePageAsync()` method is still async, so you still can use my solution in your enclosing method – Tobias Thieron Apr 10 '21 at 08:40
  • Thanks Tibias, I will mark your answer as correct. Any idea why my question was downvoted? Was it such a bad question? I saw there were other suggestions about Async but did a search and didn't find any that gave advice that I needed. – Richard Scholey Apr 10 '21 at 08:43
  • Thank you! Well, maybe because it is kind of a duplicate regarding the topic of `async` and `await`. But asking an individual question should be still allowed IMHO. – Tobias Thieron Apr 10 '21 at 09:51
  • I'm not suggesting at all that you're doing any check incorrectly - my observation contains no `if` so it checks nothing. I was pointing out that, as your code stands, it will raise a warning/error like *"This async method lacks 'await' operators and will run synchronously."*. You mark a method with `async` when you want to use `await` within it. A body of `return true` awaits nothing. **Inside** the method, remove the await and return a completed Task that wraps a boolean. This is all nothing to do with Tobias suggestion to `await` **outside** the method – Caius Jard Apr 10 '21 at 11:32