2016-11-30 12 views
0

私はAPIからデータを取り出す機能を持っています(下記)。それをデシリアライズする行にブレークポイントを設定すると、大きなデータが入力されていることがわかります。続行タスクが完了するのを待たずに

私が続行すると、2番目の機能(下記)に入り、エラーが発生します。エラーの横にはNot yet computedと表示され、例外がスローされます。

私は小さなリストでそれを行うと、ちょうどいい(私はそれが小さなデータセットであると推測します)。

ContinueWith(タスクの完了を待っています)を使用している場合、どうすれば可能ですか?

public static async Task<Data> GetAllCardsInSet(string setName) 
    { 
       setName = WebUtility.UrlEncode(setName); 
       var correctUri = Path.Combine(ApiConstants.YugiohGetAllCardsInSet, setName); 
       Console.WriteLine(); 
       using (var httpClient = new HttpClient()) 
       { 
        var response = 
         await httpClient.GetAsync(correctUri); 
        var result = await response.Content.ReadAsStringAsync(); 
        var cardData = JsonConvert.DeserializeObject<CardSetCards>(result); 
        for (int i = 0; i < cardData.Data.Cards.Count; i++) 
        { 
         cardData.Data.Cards[i] = FormatWords(cardData.Data.Cards[i]); 
        } 
        return cardData.Data; 
       } 
    } 


    private void GetYugiohCardsAndNavigate(string name) 
    { 
    var cardSetData = YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name).ContinueWith((result) => 
       { 
        //var cards = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name); 
        try 
        { 
         this.mainPage.NavigateToYugiohCardListPage(result.Result); 
        } 
        catch (Exception e) 
        { 
         HelperFunctions.ShowToastNotification("Trading Card App", "Sorry, we could not fetch this set"); 
        } 

       }); 
} 
+0

? – kat1330

+0

null参照例外が返されます –

+0

'cardData.Data'の代わりに' await Task.FromResult(cardData.Data) 'を返すとどうなるか調べてください。 – kat1330

答えて

2

GetAllCardsInSetメソッドを変更する必要はありません。
ただし、このメソッドの使用はリファクタリングすることができます。
方法GetAllCardsInSetTaskを返し、この完了を確認していないTask
Taskが完了していることを確認する必要があります。awaitキーワードを使用するのが最も簡単です。待ちタスクは戻り値をunwrappするか、タスクが例外なく完了した場合に例外をスローします。 aynchronousへGetYugiohCardsAndNavigate変更メソッドシグネチャにasync/awaitを使用して返すため

Task

private async Task GetYugiohCardsAndNavigate(string name) 
{ 
    try 
    { 
     var cardSetData = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name); 
     this.mainPage.NavigateToYugiohCardListPage(cardSetData); 
    } 
    catch (Exception e) 
    { 
     HelperFunctions.ShowToastNotification("Trading Card App", 
               "Sorry, we could not fetch this set"); 
    } 
} 
あなたは `ContinueWith`はただ待つ` GetAllCardsInSet`方法を避ける場合が起こったであろう何
0

Waitを使用せずにsyncメソッドで非同期メソッドを呼び出しました。

  YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name).ContinueWith((result) => 
      { 
       //var cards = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name); 
       try 
       { 
        this.mainPage.NavigateToYugiohCardListPage(result.Result); 
       } 
       catch (Exception e) 
       { 
        HelperFunctions.ShowToastNotification("Trading Card App", "Sorry, we could not fetch this set"); 
       } 

      }).Wait(); 
関連する問題