简体   繁体   English

Windows Phone 8 RestSharp请求。 异步/等待

[英]Windows phone 8 RestSharp request. Async/await

I know it has been asked a lot, but my problem is, that my method won't wait for the request to be completet, even though i have implemented a TaskCompletionSource, which should have done the job, but it doesn't. 我知道已经问了很多问题,但是我的问题是,即使我实现了TaskCompletionSource,我的方法也不会等待请求完成,但事实并非如此。

public DecksViewModel(bool local)
{
    DList = new List<Deck>();

    if (local)
        InitializeLocalDeckList();
    else
    {
        Dereffering();
    }
}

public async void Dereffering()
{
    var e = await InitilaizeWebDeckList();
    List<DeckIn> decksIn = JsonConvert.DeserializeObject<List<DeckIn>>(e);
    foreach (DeckIn d in decksIn)
    {
        Deck dadd = new Deck();
        dadd.CardCount = 0;
        dadd.Name = d.name;
        dadd.PicturePath = d.image;
        dadd.InstallDirectory = false;
        DList.Add(dadd);
    }

    DataSource = AlphaKeyGroup<Deck>.CreateGroups(DList, System.Threading.Thread.CurrentThread.CurrentUICulture, (Deck s) => { return s.Name; }, true);
}

public Task<String> InitilaizeWebDeckList()
{
    var tcs = new TaskCompletionSource<string>();
    var client = new RestClient("blabla.com");
    var request = new RestRequest("");
    request.AddHeader("Authorization", "Basic blabla");
    client.ExecuteAsync(request, response =>
    {
        test = response.Content;
        tcs.SetResult(response.Content);


    });
    return tcs.Task;
}

So when I call the DecksViewModel constructor, I asyncally try to request the data from a webserver and fill the model. 因此,当我调用DecksViewModel构造函数时,我异步尝试从Web服务器请求数据并填充模型。 The point is, that the corresponding view "doesn't wait" for the request to fill the model, so it's displayed empty. 关键是,相应的视图“不等待”填充模型的请求,因此显示为空。 I use the 我用

List<AlphaKeyGroup<Deck>> DataSource 

to fill a LongListSelector via DataBinding. 通过DataBinding填充LongListSelector。 But DataSource isn't yet set, when it is binded. 但是,绑定时,DataSource尚未设置。 I hope you can help 希望你能帮忙

You're calling an async method without awaiting it inside the constructor. 您正在调用异步方法,而无需在构造函数中等待它。 That's why "it doesn't wait" (because it has nothing to wait on). 这就是为什么“它不等待”的原因(因为它没有什么可以等待的)。

It's usually a bad idea to call an async method inside the constructor for that reason combined with the fact that constructors can't be async . 因此,在构造函数内部调用async方法通常是一个坏主意,同时考虑到构造函数无法async的事实。

You should redesign your solution accordingly. 您应该相应地重新设计解决方案。 An option is to have an async static method that creates an instance and await s the procedure: 一种选择是拥有一个async静态方法,该方法创建一个实例并await该过程:

public static async Task CreateInstance(bool local)
{
    var model = new DecksViewModel();
    if (local)
    {
        await InitializeLocalDeckList();
    }
    else
    {
        await Dereffering();
    }
}

That would allow you to not use async void which should only be used in UI even handlers. 那将允许您不使用仅在UI甚至处理程序中使用的async void

You can read more about other options in Stephen Cleary's blog 您可以在Stephen Cleary的博客中了解其他选项的更多信息

You are using async void , which means nobody's gonna wait for that. 您正在使用async void ,这意味着没有人会等待。 It's just fire and forget . 只是失火而忘记

I see some misunderstanding in the async keyword here: Your code will only wait for the result of an async method, if you use await. 我在这里的async关键字中看到一些误解:如果使用await,您的代码将仅等待async方法的结果。 Otherwise that call will just start the async method, but you don't know when it is actually gonna run. 否则,该调用将仅启动async方法,但您不知道它何时真正运行。

You cannot use await in constructors though. 但是,您不能在构造函数中使用await。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM