我是响应式编程的新手,遇到了一个问题......
我的代码如下所示:
IsBusy = true;
service.BeginGetClients(param, c =>
{
var r = service.EndGetClients(c);
if(!CheckResult(r))
{
service.BeginGetCachedClients(param, c2 =>
{
var r2 = service.EndGetCachedClients(c2);
if(CheckResult(r2))
ShowMessage("clients valid");
else
ShowMessage("clients not valid");
UpdateClients(r2);
service.BeginUpdateClients(r2, c3 =>
{
var b = service.EndUpdateClients(c3);
if(b)
ShowMessage("clients updated");
else
ShowMessage("clients not updated");
IsBusy = false;
}, null);
}, null);
}
else
{
ShowMessage("error on get clients");
IsBusy = false;
}
}, null);
怎么改成fluent Rx?我从这段代码开始:
var invokeClients = Observable.FromAsyncPattern<Param, List<Client>>(service.BeginGetClients, service.EndGetClients);
var invokeCachedClients = Observable.FromAsyncPattern<Param, List<Client>>(service.BeginGetCachedClients, service.EndGetCachedClients);
invokeClients(param)
.Subscribe(r =>
{
if(!CheckResult(r))
{
invokeCachedClients(param)
.Subscribe(r2 =>
{
// TODO: next op
});
}
});
有任何改进此代码的建议吗?也许另一种解决方案?我不喜欢这种级联代码...
谢谢!