在同一 IObservable 订阅内访问 IObservable
这是我尝试使用响应式扩展执行的简单示例,但它不起作用
Add 在这个简单示例中不起作用
public static void Main(string[] args)
{
var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
IDisposable subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
{
Console.WriteLine(p.ToString());
Console.WriteLine(Add(obs).ToString());
},
err => Console.WriteLine("Error"),
() => Console.WriteLine("Sequence Completed")
);
Console.ReadLine();
subscription.Dispose();
}
private static int Add(IObservable<int> wholeList)
{
int sum = 0;
wholeList.ForEach(i => sum = sum + i);
return sum;
}
实际输出
1
_
所需输出
1
6
2
6
3
6
Sequence Completed
_
即我想执行方法 Add(obs )在每次迭代中,其中 obs 本身就是正在进行迭代的冷 IObservable?
Here is a bare-bones example of what I'm trying to do with Reactive Extensions, but it does not work
Add does not work in this simple example
public static void Main(string[] args)
{
var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
IDisposable subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
{
Console.WriteLine(p.ToString());
Console.WriteLine(Add(obs).ToString());
},
err => Console.WriteLine("Error"),
() => Console.WriteLine("Sequence Completed")
);
Console.ReadLine();
subscription.Dispose();
}
private static int Add(IObservable<int> wholeList)
{
int sum = 0;
wholeList.ForEach(i => sum = sum + i);
return sum;
}
Actual Output
1
_
Desired Output
1
6
2
6
3
6
Sequence Completed
_
i.e. i would like to execute a method Add(obs) inside each iteration, where obs is itself the cold IObservable undergoing the iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为:
您应该注意到,就 Rx 而言,您正在做一件坏事。你正在进出可观察对象。您应该尽可能避免这种情况。
因此,例如,避免这种情况:
当这是相同的时:
整个
static int Add(IObservableWholeList)
方法也很糟糕。它调用ForEach
(这通常应该是一个警告,表明您做错了什么)从可观察的值中取出值。这就是可能发生死锁的地方。已经有一个名为
Sum
的可观察扩展,它返回一个IObservble
并且这不会让您脱离可观察。因此,尝试像这样编写代码:
我希望这会有所帮助。
Change this:
to this:
You should note that you're doing a bad thing as far as Rx is concerned. You are moving in and out of the observables. You should avoid this where ever possible.
So, for example, avoid this:
when this is the same:
Also the whole
static int Add(IObservable<int> wholeList)
method is bad. It callsForEach
(which generally should be a warning that you're doing something wrong) to take the values out of an observable. This is where dead-locking can occur.There already is an observable extension called
Sum
which returns anIObservble<int>
and this doesn't take you out of the observable.So try writing your code like this:
I hope this helps.
根据您的评论,我建议您根据需要创建可观察的项目来生成项目,而不是在订阅后执行此操作。在您的示例中,您可以执行以下操作:
As par your comment I would suggest that you make the observable to generate the items as required and not do this stuff after subscription. In you example you can do something like: