c#-在ReactiveUI ViewModel中取消异步任务(ReactiveObject)
作者:互联网
我目前正在使用ReactiveUI(5.5.1)进行实验,并创建了一个ViewModel(ReactiveObject的子类),它可以自动完成位置搜索(改编自mikebluestein/ReactiveUIDemo on github).每次查询文本更改时,都会调用REST服务,该服务返回所提交查询的匹配位置.
问题:正如您在下面的代码中看到的那样,可以取消DoSearchAsync(string query,CancellationToken cancelestToken),但是,我不确定如何(以及在代码中的位置)实际取消任何搜索-因此使用CancellationToken.None atm.在这种特定用例中取消请求似乎是一个过大的杀手,但我想知道无论如何在这种reactUI / async-Task场景中如何启用取消.
码:
public class ReactiveLocationSearchViewModel : ReactiveObject {
readonly ReactiveCommand searchCommand = new ReactiveCommand();
public ReactiveCommand SearchCommand { get { return searchCommand; } }
string query;
public string Query
{
get { return query; }
set { this.RaiseAndSetIfChanged(ref query, value); }
}
public ReactiveList<Location> ReactiveData { get; protected set; }
public ReactiveLocationSearchViewModel()
{
ReactiveData = new ReactiveList<Location>();
var results = searchCommand.RegisterAsyncTask<List<Location>>(async _ => await DoSearchAsync(query, CancellationToken.None));
this.ObservableForProperty<ReactiveLocationSearchViewModel, string>("Query")
.Throttle(new TimeSpan(700))
.Select(x => x.Value).DistinctUntilChanged()
.Where(x => !String.IsNullOrWhiteSpace(x))
.Subscribe(searchCommand.Execute);
results.Subscribe(list =>
{
ReactiveData.Clear();
ReactiveData.AddRange(list);
});
}
private async Task<List<Location>> DoSearchAsync(string query, CancellationToken cancellationToken)
{
// create default empty list
var locations = new List<Location>();
// only search if query is not empty
if (!string.IsNullOrEmpty(query))
{
ILocationService service = ServiceContainer.Resolve<ILocationService>();
locations = await service.GetLocationsAsync(query, cancellationToken);
}
return locations;
}
}
解决方法:
RxUI 5.x没有内置的功能,但是很容易伪造:
var results = searchCommand.RegisterAsync<List<Location>>(
_ => Observable.StartAsync(ct => DoSearchAsync(query, ct)));
在RxUI v6中,这是内置的:
searchCommand = ReactiveCommand.CreateAsyncTask(
(_, ct) => DoSearchAsync(query, ct));
标签:cancellation,reactiveui,c,mvvm 来源: https://codeday.me/bug/20191121/2053328.html