问题描述
你好,我正在尝试将项目异步添加到列表中,但是我不确定这样做是如何完成的,如果我使用的语法不正确,这就是我目前的情况:
Hello I am trying to add items to a list asynchronously but I am not sure how it is done and if I am using an incorrect syntax, this is what I have at the moment:
我的观点:
await viewModel.getMessages();
list.ItemsSource = viewModel.Messages;
我的视图模型:
public List<Message> Messages { get; set; }
public async Task getMessages()
{
await GetRemoteMessages();
}
private async Task GetRemoteMessages()
{
var remoteClient = new ChiesiClient();
var messages = await remoteClient.getMessages().ConfigureAwait(false);
//App.Database.
await SaveAll(messages).ConfigureAwait(false);
}
public async Task SaveAll(IEnumerable<Message> _messages)
{
foreach (var item in _messages)
{
await SaveMessage(item);
}
}
public async Task SaveMessage(Message item)
{
await Messages.Add(new Message // Cannot await void
{
Title = item.Title,
Description = item.Description,
Date = item.Date,
Url = item.Url
});
}
我不确定我是否能正确理解整个概念,但是如何等待将其添加到列表中?正确的方法是怎么做到的?
I am not sure if I am getting the whole concept right but how can I await adding items to my list? how is the correct way to do this?
推荐答案
您不想想要将项目异步添加到列表中-并没有真正的好处.
You don't want to add items to a list asynchronously -- there's no real benefit to it.
您的代码很好,您只需从远程服务获取消息后就同步进行所有操作.
Your code is fine, you just need to do everything synchronously after you get your messages back from your remote service.
public List<Message> Messages { get; set; }
public async Task getMessages()
{
await GetRemoteMessages();
}
private async Task GetRemoteMessages()
{
var remoteClient = new ChiesiClient();
var messages = await remoteClient.getMessages().ConfigureAwait(false);
//App.Database.
SaveAll(messages);
}
public void SaveAll(IEnumerable<Message> _messages)
{
foreach (var item in _messages)
{
SaveMessage(item);
}
}
public void SaveMessage(Message item)
{
Messages.Add(new Message // Cannot await void
{
Title = item.Title,
Description = item.Description,
Date = item.Date,
Url = item.Url
});
}
现在, remoteClient.getMessages()
可能需要很长时间!这是一个受I/O约束的任务-可能需要一毫秒,或者可能需要10分钟.我们不知道,我们也不知道.如果确实要花10分钟,那么我们不希望UI在整个时间内都被锁定.当您使用 await
关键字时,它将启动该操作,然后将控制权交还给UI.操作完成后,将继续该方法的其余部分.
Now, remoteClient.getMessages()
might take a long time! It's an I/O bound task -- it might take a millisecond, or it might take 10 minutes. We don't know, and we can't know. If it actually does end up taking 10 minutes, we don't want the UI to be locked up that entire time. When you use the await
keyword, it starts that operation, then gives control back to the UI. When the operation is done, it continues with the rest of the method.
您要处理的原始示例将内容保存到数据库.那是另一个受I/O约束的操作,因此数据库API创建者请为您提供一些异步API来帮助您解决这个问题.
The original example you're working off of saves stuff to a database. That's another I/O bound operation, and thus the database API creators kindly provided you with some asynchronous APIs to help out with that.
您正在修改示例以将结果放入列表中.添加到列表不是I/O绑定的操作.实际上,它几乎是瞬时的.结果,没有异步API.您不需要异步执行该部分.
You're modifying the example to put the results into a list. Adding to a list is not an I/O bound operation. In fact, it's pretty much instantaneous. As a result, there's no asynchronous API for it. You don't need to do that part asynchronously.
这篇关于List.add()异步任务等待正确的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!