Asynchronous task in the MvvmCross command does not return

I am creating a small project using MvvMCross in a PCL Xamarin project and having a problem with the async Task that I invoke in the button-related command.

I have a fake web service where I just call Task.Delay (3000). When the process comes to this point, it just sits and does nothing.

I initially had a command call using the .wait () call, but read somewhere that it was a blocking call and could not be sliced ​​async / wait

Can someone help and give me a hint about where I am going wrong by linking a team?

https://bitbucket.org/johncogan/exevaxamarinapp is a public git repo, specific command

public ICommand SaveProfile

in the ProfileViewModel.cs file.

Specific code:

public ICommand SaveProfile
    {
        get
        {
            return new MvxCommand(() =>
            {
                if (_profile.IsValidData())
                {
                    // Wait for task to compelte, do UI updates here
                    // TODO Throbber / Spinner
                    EnumWebServiceResult taskResult;
                    Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).Wait();

                    if(_profileWebService.getLastResponseResult() == true){
                        taskResult = EnumWebServiceResult.SUCCESS;
                    }else{
                        taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
                    }
                    //_profileWebService.SendProfileToServer(_profile).Wait();
                    // Close(this);
                }
            });
        }
    }

Web Service Class ():

using System;
using System.Threading.Tasks;
using ExevaXamarinApp.Models;

namespace ExevaXamarinApp.Services
{
public class FakeProfileWebService : IProfileWebService
{
    public int _delayPeriod { get; private set; }
    public bool? lastResult;

    /// <summary>
    /// Initializes a new instance of the <see cref="T:ExevaXamarinApp.Enumerations.FakeProfileWebService"/> class.
    /// </summary>
    /// 3 second delay to simulate a remote request
    public FakeProfileWebService()
    {
        _delayPeriod = 3000;
        lastResult = null;
    }

    private Task Sleep()
    {
        return Task.Delay(3000);
    }

    public bool? getLastResponseResult(){
        return lastResult;
    }

    /// <summary>
    /// Sends the profile to server asynchronously
    /// </summary>
    /// <returns>EnumWebServiceResultFlag value</returns>
    /// <param name="profileObject">Profile model object</param>
    public async Task SendProfileToServer(Profile profileObject)
    {
        // Validate arguments before attempting to use web serivce
        if (profileObject.IsValidData())
        {
            // TODO: Return ENUM FLAG that represents the state of the result
            await Sleep();
            lastResult = true;
        }else{
            lastResult = false;
        }
    }
}
}
+4
1

, :

    public ICommand SaveProfile
    {
        get
        {
            return new MvxCommand(async () =>              // async added
            {
                if (_profile.IsValidData())
                {
                    // Wait for task to compelte, do UI updates here
                    // TODO Throbber / Spinner
                    EnumWebServiceResult taskResult;
                    await Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).ConfigureAwait(false);         // await, confi.. added

                    if(_profileWebService.getLastResponseResult() == true){
                        taskResult = EnumWebServiceResult.SUCCESS;
                    }else{
                        taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
                    }
                    //_profileWebService.SendProfileToServer(_profile).Wait();
                    // Close(this);
                }
            });
        }
    }

    private async Task Sleep()                                 // async added
    {
        return await Task.Delay(3000).ConfigureAwait(false);   // await, confi... added
    }

    public async Task SendProfileToServer(Profile profileObject)
    {
        // Validate arguments before attempting to use web serivce
        if (profileObject.IsValidData())
        {
            // TODO: Return ENUM FLAG that represents the state of the result
            await Sleep().ConfigureAwait(false);                      // await, confi... added
            lastResult = true;
        }else{
            lastResult = false;
        }
    }

, .

+2

Source: https://habr.com/ru/post/1677093/


All Articles