我正在尝试将两个单独功能的功能串联到一个Task中。我可能正在做书,但是我也很想知道这是否可行,所以希望那里的一些C#向导能够解决这个问题。
这是当前设置。
async Task GetData()
{
// Get some cached data immediately from the database
Data = FetchDataLocal();
// Replace the local data with the data from the web when it is available
Data = await FetchDataRemote();
}
我想将这两个函数移到一个仅返回的Task中,Data
但是一个任务当然只能返回一次。
所以本质上,我需要这种(以下)功能,但形式正确。我很乐观,那里有一个优雅的解决方案。
async Task<DataType> GetData()
{
// Get some cached data immediately from the database
return FetchDataLocal();
// Replace the local data with the data from the web when it is available
return await FetchDataRemote();
}
是否有某种Task解决方案可让您执行以下操作?因此,任务将返回两次数据(立即数据然后是延迟数据)。解决方案甚至可能不是任务,但我不确定会是什么。
假设您正在MVVM应用程序中工作,我建议使用我的NotifyTask<T>
type,它可以INotifyPropertyChanged
为您处理这些事情:
public NotifyTask<TData> Data;
ViewModelConstructor()
{
// Get some cached data immediately from the database
var immediateData = FetchDataLocal();
// Replace the local data with the data from the web when it is available
Data = NotifyTask.Create(FetchDataRemote(), immediateData);
}
如果您确实使用NotifyTask<T>
,则需要将数据绑定到Data.Result
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句