views:

190

answers:

1

Given a BehaviorSubject, what is the practical difference between calling all of these different functions on it?

  • First()
  • Last()
  • LatestValue()
  • MostRecentValue()
  • NextValue()
  • Single()
  • Take(1)

Assuming I understand it right, they should all do about the same thing, given the BehaviorSubject.

If so, then which call is the most appropriate (by which I mean: which best communicates my intent)? First or Single perhaps?

If not, then what are the practical differences?

+3  A: 

First, Last, and Single are blocking; it is recommended to use Take(1) instead, so you get an IObservable back. When chaining query operators it is recommended to avoid First, Last and Single because you exit the safety of the monad ... that is to say you have blocking side effects. See http://blogs.msdn.com/jeffva/archive/2009/12/09/first-last-contains-etc-can-be-extremely-dangerous-yet-extremely-useful.aspx for more about that.

MostRecentValue and LatestValue have been removed from the latest version of Rx because they are blocking as well, so the only blocking operators left are First, Last, and Single (and the xxxOrDefault variants), according to the latest release notes.

MostRecent will return the last value sampled, as often as you call it (and it takes an initialValue to guarantee it will never wait), i.e. "without consumption", whereas Latest will wait until a value arrives then return it "with consumption" - that is, if you call Latest again it will not return the same value as the last call, it will wait until the next value arrives, if ever.

Richard Hein