Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

c# - DynamicData and SourceCache<T> - auto refresh doesn't work

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...