My goal is to create a bunch of observables from a source observable, so that I can subscribe to them individually.
When I do that manually (that is, creating each subSource manually), things work as expected: the values added to the original source propagate adequately to the subSources.
But when I create them in a loop, adding them to a List<IObservable<T>>
, the subscriptions to elements taken from that list don't seem to be working:
class Program
{
static void Main(string[] args)
{
// using Subject for the sake of example
var source = new Subject<int>();
// manually creating each subSource
var source0 = source.Where((t, i) => i % 3 == 0);
var source1 = source.Where((t, i) => i % 3 == 1);
var source2 = source.Where((t, i) => i % 3 == 2);
// creating a List of subsources
List<IObservable<int>> sources = new List<IObservable<int>>();
int count = 3;
for (int i = 0; i < count; i++)
{
sources.Add(source.Where((v, ix) => ix % 3 == i));
}
// subscribing to one subSource from each group
source0.Subscribe(Console.WriteLine); // this works
sources[1].Subscribe(Console.WriteLine); // this doesn't
// feeding data
Observable.Range(0, 20).Subscribe(source);
Console.ReadKey();
}
}