Home > Net >  How to attach a Task<List<T>> to a List<T>?
How to attach a Task<List<T>> to a List<T>?

Time:11-14

Trying setting up my SQLite data and populating from database to Lists database will return an async Task<List<T>> that holds values I need to assign to my collection list. I get error code CS1503 :

Using System.Collections.Generic;
Using SQLite;

public Task<List<Reward>> rewards;
public List<Reward> rewardsList;
RewardDatabase reward;

private async void GetRewards()
{
    rewards = reward.GetItemsAsync();
}

async void AssignIt()
{
    rewardsList = await rewards;
}

I am trying to get values from rewards to a collection I can access from rewardsList.

CodePudding user response:

Use await when calling async methods

rewardlist = await reward.GetItemsAsync();

CodePudding user response:

In this case, you could just use task.Result to get List<T> (in document):

// remove `async`
private void GetRewards()
{
    rewards = reward.GetItemAsync();
    rewardList = rewards.Result;
}

or use async/await:

private async void GetRewards() 
{
    rewards = reward.GetIemAsync();
    rewardList = await rewards;
}

But first solution looks very strange, maybe RewardDatabase was provide like GetItem() or some other synchronized function?

  • Related