You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Bug]: When observing child property in a two-way fashion from ViewModel to Model, when the parent updates, the "old" parent gets updated with the new parent value
#3324
Open
Tetedeiench opened this issue
Jul 27, 2022
· 1 comment
This is tough to describe, I've added a repository to reproduce it. This issue is following a discussion on slack where this has been found almost certainly as a bug.
It happens on both Avalonia and bare WPF ( Thanks to @tomasfil )
When a viewmodel "binds" using WhenAnyValue to a child property of a model object, and the model object is replaced by a new one, the "old" model objects get set by a new object.
It is as if the "source" value of WhenAnyValue was cached, and when the object updates, it isn't reflected.
public class ScheduleViewModel : ViewModelBase
{
private SchedulePeriod _period;
public SchedulePeriod Period
{
get => _period;
set => this.RaiseAndSetIfChanged(ref _period, value);
}
private bool _isInfinite;
public bool IsInfinite
{
get => _isInfinite;
set => this.RaiseAndSetIfChanged(ref _isInfinite, value);
}
public ReactiveCommand<SchedulePeriod, Unit> SelectPeriodCommand { get; }
(...)
public ScheduleViewModel()
{
this.WhenAnyValue(x => x.Period.IsInfinite)
.DistinctUntilChanged()
.ObserveOn(RxApp.MainThreadScheduler)
.BindTo(this, x => x.IsInfinite);
//viewmodel to model
this.WhenAnyValue(x => x.IsInfinite)
.Skip(1)
.DistinctUntilChanged()
.ObserveOn(RxApp.MainThreadScheduler)
.BindTo(this, x => x.Period.IsInfinite);
(...)
}
}
Let's imagine, in the current state, the IsInfinite bool is true.
The user now invokes the SelectPeriodCommand through a button. It changes the selected period in the viewmodel, thus the "Period" property is mutated.
The new period being set has "IsInfinite" set to false by default.
This triggers the two WhenAnyValue pipeline.
It seems that the second pipeline fires, and updates the previous model object, and set its IsInfinite property to false, instead of binding to the new object, as expected.
It is as if the "Period" value was cached and not properly updated everywhere.
See the screenshot section for a gif showing the visual representation of what happens.
Describe the bug 🐞
This is tough to describe, I've added a repository to reproduce it. This issue is following a discussion on slack where this has been found almost certainly as a bug.
It happens on both Avalonia and bare WPF ( Thanks to @tomasfil )
When a viewmodel "binds" using WhenAnyValue to a child property of a model object, and the model object is replaced by a new one, the "old" model objects get set by a new object.
It is as if the "source" value of WhenAnyValue was cached, and when the object updates, it isn't reflected.
Here's an example in a repro repository : https://github.com/Tetedeiench/reactiveUiLiveModelEditIssue
Let's imagine, in the current state, the IsInfinite bool is true.
The user now invokes the SelectPeriodCommand through a button. It changes the selected period in the viewmodel, thus the "Period" property is mutated.
The new period being set has "IsInfinite" set to false by default.
This triggers the two WhenAnyValue pipeline.
It seems that the second pipeline fires, and updates the previous model object, and set its IsInfinite property to false, instead of binding to the new object, as expected.
It is as if the "Period" value was cached and not properly updated everywhere.
See the screenshot section for a gif showing the visual representation of what happens.
Step to reproduce
Reproduction repository
https://github.com/Tetedeiench/reactiveUiLiveModelEditIssue
Expected behavior
The "old" Period object should not be mutated, and x.Period should resolve to the most up-to-date value to prevent this behavior
Screenshots 🖼️
IDE
Visual Studio 2022, Rider Windows
Operating system
Windows
Version
11 and others probably
Device
PC
ReactiveUI Version
18.3.1
Additional information ℹ️
No response
The text was updated successfully, but these errors were encountered: