Aync / await ...Warnings!!

legariolegario Member ✭✭
edited February 28 in Xamarin.Forms

Hi, I have some problems to understand async/await... My app its workin ok but I have warning in the compiler when I call async methods ()
In MyPage I have this event with some "if" for diferent pages . Y try to change the event to "async Task" but wen I tap on the item the code its not working and don´t go to the page that Im want. So I leave the code like you see, the app its working but with the warning on the compiler

private  void AlimentosList_ItemTappedAsync(object sender, ItemTappedEventArgs e)
        {
              if (e.Item as string == null)
            { 

                return;
            }
            else  
            {
                AlimentosList.ItemsSource = alimento.Where(c => c.Equals(e.Item as string));
                SearchContent.Text = e.Item as string;
                if (e.Item as string == "Arveja")
                {
                     var pageToPush = new CorazonPage();
                     //here the compiler shows a warning because I call to async method with out await.
                     pageToPush.Llamar_Arveja(); 
                     Navigation.PushAsync(pageToPush);

                }`
//here is the method that I calling ,,,and all the code is working ok in my phone
`public async Task Llamar_Arveja()
        {
            //await Search_Aguacate();
            await ScrollOnceInLayoutAsync(ScrollCorazon, Buscar_Arveja, true, ScrollToPosition.Start);
        }
Tagged:

Best Answers

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    If you change the method signature to private async void then you can await your Tasks.

  • legariolegario Member ✭✭

    Hi @JoeManke
    I did that before but don't work , when I push to the new page and then is activate the scrollHelper to move the screen where I want, the screen don't move
    With the code that you see its workin ok but with all the warning on the compiler

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Maybe you should wait until after pushing the page onto the navigation stack before you try to scroll in it.

  • legariolegario Member ✭✭

    I have placed a delay but it still does not work, with this code the compiler does not give me any warning message but the app does not work: / @JoeManke

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 1

    @legario

    @JoeManke is correct:

    private async void AlimentosList_ItemTappedAsync(object sender, ItemTappedEventArgs e)

    await Navigation.PushAsync(pageToPush);

    And, it is intended that await ScrollOnceInLayoutAsync (which I am assuming is an implementation I posted in the forums) be called from the OnAppearing() override of CorazonPage, not from AlimentosList_ItemTappedAsync . I'd have to look at the code again to see whether it can be safely called in the way that you have done so, but I did not intend for it to be used in the way that you have.

    Note that depending on your particular scenario, you might only want to call ScrollOnceInLayoutAsync the first time that OnAppearing() is called.

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 1

    @ShantimohanElchuri said:
    The "async void" is allowed only for events.

    That's incorrect. The usual recommendation is that it is only used for events, but it is "allowed" to be used for other things. Whether it's wise to use it for other things is another matter.

    @ShantimohanElchuri said:
    If an 'async' method is called without 'await', then that call will be executed as a 'sync' method.

    That's open to misinterpretation. What happens if you do the following?

    var v = DoSomething();
    Task.Delay(5000); // note that I intentionally missed off the await
    var w = DoSomethingElse();
    

    or

    var v = DoSomething();
    Task.Run(() => // note that I intentionally missed off the await
    {
        var y = DoSomethingElseAgain();
    });
    var w = DoSomethingElse();
    

    @ShantimohanElchuri said:
    You don't have to name the event with Async prefix.

    True, but it makes code readability so much better if async methods are consistently postfixed with "Async".

  • legariolegario Member ✭✭

    Thanks for your help guys @JohnHardman and @ShantimohanElchuri
    In the end I realized that I was calling the event first ( await pageToPush.Llamar_Arveja(); )and then doing the pushAsync
    then the page, was loaded after calling the ScrollAsync method so the screen did not scroll to the desired location. My fault, I am blind from fatigue but the code was always there waiting...
    Now the code compiles perfect without warnings or errors.

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 1

    @legario said:
    Thanks for your help guys @JohnHardman and @ShantimohanElchuri
    In the end I realized that I was calling the event first ( await pageToPush.Llamar_Arveja(); )and then doing the pushAsync
    then the page, was loaded after calling the ScrollAsync method so the screen did not scroll to the desired location. My fault, I am blind from fatigue but the code was always there waiting...
    Now the code compiles perfect without warnings or errors.

    So you have done all the bits highlighted in bold at https://forums.xamarin.com/discussion/comment/366382/#Comment_366382 ( those same bits mentioned by @JoeManke ) ? If not, you may still have issues in your code. Post your updated code if you are still uncertain.

  • legariolegario Member ✭✭
    edited March 2

    Hi @JohnHardman , I just changed the event to "async void" and called with "await", my error was the order of the call to the method that I passed it just after doing the NavigationPushAsync.
    The app it going well now, with out warnings.

       private async void AlimentosList_ItemTappedAsync(object sender, ItemTappedEventArgs e)
            {
    
            if (e.Item as string == null)
            {
    
                return;
    
            }
    
            else
    
            {   
                AlimentosList.ItemsSource = alimento.Where(c => c.Equals(e.Item as string));
                SearchContent.Text = e.Item as string;
                if (e.Item as string == "Arveja")
                {
    
                    var pageToPush = new CorazonPage();
            //here was my error, this two lines I hade change the order 
                    await Navigation.PushAsync(pageToPush);
                    await pageToPush.Llamar_Arveja();
    
    
    
                }
                if (e.Item as string == "Aguacate")
                {
    
                    var pageToPush = new CorazonPage(); 
                    await Navigation.PushAsync(pageToPush);
                    await pageToPush.Llamar_Aguacate();
    
                }
    

    }`

Sign In or Register to comment.