I have a question which is an extension of the following question raised on this site.
Is there a more elegant way to merge observables when return type is unimportant?
I have an IObservable<Unit>
(lets say X
), a reactive collection (Y
) and a property (Z
). Return type is not important. I just want to subscribe when any of these change.
I know how to observe all 3 and Subscribe
using Observable.Merge
as below.
Observable.Merge(X, Y.Changed, ObservableForProperty(Z).Select(_ => Unit.Default)).Subscribe(..)
And it works.
However, when I try to use WhenAny(...,....,....).Subscribe()
, the subscribe does not get triggered when my X
changes. What is the syntax for doing the above using WhenAny(...)
rather than Observable.Merge(..)
??
I prefer to use WhenAny(....)
because I am using ReactiveUI
in other places.
Example:
Say I've got a class derived from ReactiveObject
with following properties.
public class AnotherVM : ReactiveObject
{
public bool IsTrue
{
get { return this.isTrue; }
set { this.RaiseAndSetIfChanged(x => x.isTrue, ref this.isTrue, value); }
}
public IObservable<Unit> Data
{
get { return this.data; }
}
public ReactiveCollection MyCol
{
get { return Mycol; }
}
}
public class MyVM : ReactiveObject
{
MyVM
{
// do WhenAny or Observable.Merge here....
}
}
I want to observe the above properties in AnotherVM
class using Observable.Merge(..)
or WhenAny(...)
in MyVM
class. I found that I do not always get a notification when I subscribe to the above in MyVM
using WhenAny(...)
or Merge(...)
when either of the 3 properties change.