I have following method which will eventually return some Task<IList<DataModel>>
but for now just returns null
. I want to load result of this list to ObservableCollection in my ViewModel which is then displayed in a ListView.
But for now, I just want to return null and check that that is handled properly, so my ListView should show nothing in it as a result. I simmulate that by this code:
public async Task<IList<DatatModel>> GetData(string id)
{
return await Task.FromResult<IList<DataModel>>(null);
}
I call the code above and will loop through result of my Task and load it all in my ObservableCollection like so:
public void Initialize()
{
foreach (var data in GetData(Id).Result)
{
MyObservableCollection.Add(data);
}
}
However, my app just freezes. I think that above call to GetData(id).Result is problem because Result is null. How do I loop through this data and load it into my ObservableCollection if some data exist, or simply dont load anything if there is no data returned?