12

Using ReactiveUI, is it possible to ignore the initial value for a given ReactiveObject?

For example, I have a ViewModel I initialize, and then I WhenAnyValue on the ViewModel. I get notified immediately that the value is null for my chosen property. Yes, I could .Where(x => x != null) to avoid this, but it's potentially possible that knowing it's null later is of value.

In other words, I want to start getting notifications after the 1st change in the value.

I'm not readily seeing how I can do this or if it's even possible. I see references to Initial Value in the source for WantsAnyValue/WantsAny but it's unclear to me how I set that initial value.

AJ Venturella
  • 4,742
  • 4
  • 33
  • 62
  • 1
    maybe not the best way, but you could Skip(1) https://msdn.microsoft.com/en-us/library/vstudio/bb358985%28v=vs.100%29.aspx – kenny Apr 14 '15 at 20:36
  • @kenny I think `Skip` is a good answer for this problem. – christophano Apr 14 '15 at 21:01
  • 1
    I'm gonna go with this as the answer. As the author or ReactiveUI [here](http://stackoverflow.com/a/22215041/1060314) also recommends this approach. @kenny could you make this a formal answer so I can mark it? – AJ Venturella Apr 17 '15 at 14:02

3 Answers3

14

Moving my comment to an answer on request of OP for points ;)

To ignore the first OnNext at init.

this.WhenAnyValue( model => model.Field ).Skip( 1 )
kenny
  • 21,522
  • 8
  • 49
  • 87
2

You should use ObservableForProperty in this case.

WhenAnyValue will immediately notify about the initial value to the subscriber plus any new values in the future while ObservableForProperty only notifies about new values and skips the initial value.

Tiago
  • 737
  • 5
  • 20
  • 2
    This does solve the problem. One additional question, per the comments per [this question](http://stackoverflow.com/a/22215041/1060314) See Clyde's comment followed by Paul Betts comment (2nd and 3rd respectively). Paul Betts is saying `Skip(1)` is preferable. I am more apt to believe `ObservableForProperty` is the better choice but I was curious, so I'll follow up with that question. Ahhhh.. I am also seeing Paul Betts is one of the Primary Authors of ReactiveUI, so I'm guessing he knows. – AJ Venturella Apr 15 '15 at 19:26
0

I think ObservableForProperty is what you're looking for. It has a "skipInitial" parameter.

Foole
  • 4,754
  • 1
  • 26
  • 23