Hi Wes,

 

I suspect there's a problem with the automatic invocation of Dispose when OnCompleted is called, when we use Linq-to-Rx. Details:

 

The MSDN Library has an example for using IObservable here: http://msdn.microsoft.com/en-us/library/dd783449(v=VS.100).aspx"> http://msdn.microsoft.com/en-us/library/dd783449(v=VS.100).aspx. There the EndTransmission method of the observable (LocationTracker) makes a copy of the observers collection (using ToArray) before calling

OnCompleted on each observer. It has to create this copy because the OnCompleted method is followed by an automatic call to Dispose.

[This automatic call happens only when we subscribe to the output of a LINQ method such as Select etc, and not directly subscribe to the

LocationTracker object (the original observable).] And Dispose removes the observer from the observers collection, so we get the exception “Collection was modified; enumeration operation may not execute.” on the next iteration in the foreach in OnCompleted. To prevent this the MSDN example uses a copy of the observers collection.

 

The problem with this approach is that an unnecessary copy is created solely to facilitate removal of the observer from the collection member variable. However, notice right there that the last line of EndTransmission is a call to observers.Clear(). So why not leave the work of clearing the collection to the observable class and you (the RX framework) avoid making the call to Dispose internally? That way we wouldn't have to make a copy of the observers collection and at the same time be able to use foreach in EndTransmission.

 

The observer class here (LocationReporter) has one string member variable instName (apart from the Unsubscriber reference). Making copies of a large number of observers makes that many unnecessary copies of these member variables. Since (from your videos) the sole purpose of Dispose here seems to be unsubscribing, we don't need to call Dispose here because the mass unsubscription is anyway achieved by the observable when it calls observers.Clear() in EndTransmission.

 

What are your thoughts on this?

 

-Venkatesh