Possible to ignore the initial value for a ReactiveObject?
Asked Answered
H

3

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.

Hipparch answered 14/4, 2015 at 20:33 Comment(3)
maybe not the best way, but you could Skip(1) msdn.microsoft.com/en-us/library/vstudio/…House
@House I think Skip is a good answer for this problem.Jillene
I'm gonna go with this as the answer. As the author or ReactiveUI here also recommends this approach. @House could you make this a formal answer so I can mark it?Hipparch
H
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 )
House answered 17/4, 2015 at 14:31 Comment(0)
D
3

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.

Dock answered 14/4, 2015 at 21:47 Comment(1)
This does solve the problem. One additional question, per the comments per this question 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.Hipparch
H
0

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

Halcomb answered 14/4, 2015 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.