I have SourceCache populated with some data. I need to filter this data based on the category selected by the user. I tried to use AutoRefreshOnObservable
and pass Subject<Unit>
to trigger refresh, however it doesn't work (auto refresh is not triggering and output collection doesn't change):
enum Category
{
Even,
Odd
}
class MyDto
{
public int Number { get; set; }
public Category Category { get; set; }
}
[Fact]
public void Test()
{
ReadOnlyObservableCollection<MyDto> filteredDtos;
SourceCache<MyDto, int> source = new SourceCache<MyDto, int>(x => x.Number);
source.AddOrUpdate(Enumerable.Range(1, 10).Select(x => new MyDto()
{
Number = x,
Category = x % 2 == 0 ? Category.Even : Category.Odd
}));
Category selectedCategory = Category.Even;
Subject<Unit> refresh = new Subject<Unit>();
source.Connect()
.AutoRefreshOnObservable(_ => refresh)
.Filter(x => x.Category == selectedCategory)
.Bind(out filteredDtos)
.Subscribe();
// Act & Assert
filteredDtos.All(x => x.Number % 2 == 0).Should().BeTrue(); // this is ok
// Change category and trigger refresh
selectedCategory = Category.Odd;
refresh.Next();
filteredDtos.All(x => x.Number % 2 == 0).Should().BeFalse(); // assertion fails
}
What am I doing wrong and how to fix it?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…