每次方法调用都可观察到的新数据 [英] new data to observable with each method invocation
本文介绍了每次方法调用都可观察到的新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这对于有专业知识的人来说可能非常简单,但是我如何直接向给定的 observable 提供新数据,每当调用我的方法时?
this may be really simple to those in the know-how, but how can i directly provide new data to a given observable, whenever a method of mine is invoked?
IObservable<int> _myObservable;
void ThingsCallMe(int someImportantNumber)
{
// Current pseudo-code seeking to be replaced with something that would compile?
_myObservable.Add(someImportantNumber);
}
void ISpyOnThings()
{
_myObservable.Subscribe(
i =>
Console.WriteLine("stole your number " + i.ToString()));
}
我也不知道我应该使用什么样的 observable,一个只有在特殊情况下才能到达 OnCompleted() 的?
i also dont know what kind of observable i should employ, one that gets to OnCompleted() under special circumstances only?
推荐答案
这是基本答案.我稍微修改了你的代码.
Here's the basic answer. I modified your code slightly.
Subject<int> _myObservable = new Subject<int>();
void ThingsCallMe(int someImportantNumber)
{
// Current pseudo-code seeking to be replaced with something that would compile?
_myObservable.OnNext(someImportantNumber);
}
void ISpyOnThings()
{
_myObservable.Subscribe(
i =>
Console.WriteLine("stole your number " + i.ToString()));
}
这应该有效.一个主题只是一个 IObservable 和一个 IObserver.您可以调用 OnCompleted、OnError 等.
This should work. A subject is simply an IObservable and an IObserver. You can call OnCompleted, OnError, etc.
这篇关于每次方法调用都可观察到的新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文