Scott Stafford
Scott Stafford

Reputation: 44776

How do I make "atomic" changes to several ReactiveUI properties?

I want to change several properties and then notify them all of changes together, so that no notifications go out until the entire state is updated. Is there a proper way to do this? If I just do:

this.Prop1 = newValue1;
this.Prop2 = newValue2;

and the property setters call RaiseAndSetIfChanged, then I'll get a notification during the invalid state. I'd love to write something like:

using (new RxTransactionScope()) {
    this.Prop1 = newValue1;
    this.Prop2 = newValue2;
}

which would cause them both to be set then both the notifications to fire.

Upvotes: 1

Views: 254

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178630

using (this.DeferChangeNotifications())
{
    this.Prop1 = newValue1;
    this.Prop2 = newValue2;
}

Upvotes: 0

class ViewModel {
   ....
    public IDisposable BeginUpdate()
    {
        return new CompositeDisposable(this.SuppressChangeNotifications(), Disposable.Create(() =>
        {
            this.RaisePropertyChanged("Prop1");
            this.RaisePropertyChanged("Prop2");
        }));
    }

using(this.BeginUpdate()) 
{
   this.Prop1 = newValue1;
   this.Prop2 = newValue2;
}

Upvotes: 2

Related Questions