反应式扩展 - 停止流动直到变量被填满
我有以下代码。我的方法有一个字符串返回类型。
public string GetRedemeptionNumber()
{
string _batchNumber = "test";
var _loadBatchName = Observable.ForkJoin(_context.QuerySingleOrDefault(
_context.GetRedemptionsQuery().Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
.OrderByDescending(x => x.ReceiveBatchName)
.Take(1)))
.Do(u => _redemptionBatch = u.FirstOrDefault())
.Select(x => new Unit())
.Finally(() =>
{
_batchNumber = _redemptionBatch.Name;
//this doesnt work since a return isnt allowed
return _batchNumber;
}
});
_loadBatchName.Subscribe();
return _batchNumber;
}
我的问题是这会贯穿并在设置之前返回我的 _batchNumber 。所以它返回一个空的_batchNumber。有没有办法阻止它运行到返回(在我的 .finally 之外)并等待 _batchNumber 从我的 .finally 中填充。
我的代码显然现在不起作用,因为我的 .finally 不允许返回。
I have the following code. My method has a string return type.
public string GetRedemeptionNumber()
{
string _batchNumber = "test";
var _loadBatchName = Observable.ForkJoin(_context.QuerySingleOrDefault(
_context.GetRedemptionsQuery().Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
.OrderByDescending(x => x.ReceiveBatchName)
.Take(1)))
.Do(u => _redemptionBatch = u.FirstOrDefault())
.Select(x => new Unit())
.Finally(() =>
{
_batchNumber = _redemptionBatch.Name;
//this doesnt work since a return isnt allowed
return _batchNumber;
}
});
_loadBatchName.Subscribe();
return _batchNumber;
}
My issue is this runs through and return my _batchNumber before it is set. so it is returning an empty _batchNumber. Is there a way to stop this from running to the return (outside of my .finally) and wait for the _batchNumber to be filled from within my .finally.
My code clearly doesnt work right now since my .finally does not allow a return in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码确实有一些问题。
首先,如果您想阻塞直到 Rx 返回,那么使用 Rx 就没有多大意义。应该是不阻塞。因此,您实际上应该返回 IObservable而不是
string
。另外,为了使您的查询具有 OrderByDescending 功能,您通常会使用可枚举的而不是可观察的,因此您的查询必须以某种方式转换为可观察的。
并且
ForkJoin
在此查询中似乎没有为您做任何事情。你为什么使用它?所有的
Do
&Finally
运算符也没有多大意义。无论如何,我无法将您的代码重构为可编译的代码,但我已经接近了。从这里开始,告诉我需要改变什么。
Your code does have a few problems.
First up if you want to block until Rx returns then there isn't much point using Rx. It should be about not blocking. So you should really return
IObservable<string>
and notstring
.Also, for your query to have
OrderByDescending
you much be using an enumerable and not an observable so somehow your query must turn into an observable.And
ForkJoin
doesn't seem to do anything for you in this query. Why did you use it?All of the
Do
&Finally
operators don't make much sense either.In any case, I couldn't refactor your code into something that compiles, but I got it close. Start with this and tell me what needs to change.