0

I suspect that my understanding of what reactive extensions are is incorrect but I would have thought that they could be used to subscribe a method call to a collection. I'm thinking of a typical event handler in dotnet. My ultimate goal is to trigger a method call whenever the collection is modified.

I have the following small console application example, as you can see in the comments I was expecting it reprint the results or at least reprint the last result when it's added to the collection. I have also tried query.ToList() to see if re-running the query would print out the results again.

   static void Main(string[] args)
    {
        // we have some data that is an IEnumerable It's lazy so we can query it.
        IEnumerable<int> data = new List<int>();

        for (int i = 0; i < 10; i++)
        {
            (data as List<int>).Add(i);
        }

        // create a linq query
        var query = from number in data select number;

        // create observable 
        IObservable<int> observableQuery = query.ToObservable<int>(Scheduler.Default);
        observableQuery.Subscribe(Console.WriteLine);



        /// the numbers get printed here 1.. 9
        Console.ReadKey();
        (data as List<int>).Add(1000);
        var l = query.ToList();
        var j = observableQuery.ToList();

        // the numbers don't get reprinted here... ? Shouldn't they reprint when the collection is modified?

        Console.ReadKey();
    }
K-Dawg
  • 3,013
  • 2
  • 34
  • 52
  • https://msdn.microsoft.com/en-us/library/hh212140(v=vs.103).aspx Going by this you don't have an error. But you have a reference issue. query and data have different reference values. When updating the data, query isn't affected. So thus the collection wasn't modified – misha130 Mar 12 '17 at 13:31
  • @misha130 I know it's not a reference. I'm trying to figure out what Rx is all about. I think from what I've been reading since posting this question is that it simply runs a query in over threads. I still don't get what all the hype is about but I'm sure that's simply because I don't understand what it is properly. – K-Dawg Mar 12 '17 at 14:50
  • If you want to react on changes in a collection you are probably better of with using an `ObservableCollection`, see http://stackoverflow.com/questions/4279185/what-is-the-use-of-observablecollection-in-net – Peter Bons Mar 12 '17 at 16:13
  • 1
    Some guidance on when to use RX: http://www.introtorx.com/content/v1.0.10621.0/01_WhyRx.html . Reactive Extensions really shine when you need to apply a time factor to your queries, for example if you have an event that has an integer as an argument you can create queries for getting all integer values of events in the last x minutes and things like that. – Peter Bons Mar 12 '17 at 16:17
  • 1
    ObservableCollection doesn't implement IObservable. It is not Rx compatible anymore than List is. – Shlomo Mar 12 '17 at 17:30
  • I think I've been a little confused as to what Rx actually is. I think I've needed ObservableCollection as Peter Bons suggests which as @Shlomo says is not part of Rx. – K-Dawg Mar 12 '17 at 18:15

1 Answers1

0

Rx isn't made for monitoring state changes in collections. The ToObservable method simply generates a new observable stream with the contents of the collection at the point in time that you call it.

If you want to have Rx operators over collections look at the DynamicData project.

Slugart
  • 4,535
  • 24
  • 32