How to produce single update on .Clear() when using .GroupOn() #621
-
|
Let's say I have a list of topics, where topic is declared as: public class Topic
{
public string Title { get; set; }
public string Category { get; set; }
public DateTime Timestamp { get; set; }
}I want to get latest topic for each category. using DynamicData;
using DynamicData.Binding;
var topics = new List<Topic>
{
new Topic { Title = "TestA1", Category = "A", Timestamp = new DateTime(2022, 7, 20, 10, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestA2", Category = "A", Timestamp = new DateTime(2022, 7, 20, 5, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestA3", Category = "A", Timestamp = new DateTime(2022, 7, 20, 7, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestB1", Category = "B", Timestamp = new DateTime(2022, 7, 20, 11, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestB2", Category = "B", Timestamp = new DateTime(2022, 7, 20, 12, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestC1", Category = "C", Timestamp = new DateTime(2022, 7, 20, 12, 0, 0, DateTimeKind.Utc) },
new Topic { Title = "TestC2", Category = "C", Timestamp = new DateTime(2022, 7, 20, 13, 0, 0, DateTimeKind.Utc) },
};
var topicSourceList = new SourceList<Topic>();
topicSourceList.AddRange(topics);
var latestTopicByCategoryObservable = topicSourceList.Connect()
.GroupOn(x => x.Category)
.TransformMany(x => x.List.Connect()
.Sort(SortExpressionComparer<Topic>.Descending(y => y.Timestamp))
.Top(1)
.AsObservableList());
latestTopicByCategoryObservable.ToCollection().Subscribe(x =>
{
Console.WriteLine($"Topic count: {x.Count}");
foreach (var topic in x)
{
Console.WriteLine($"Tile: {topic.Title}, Category: {topic.Category}, Timestamp: {topic.Timestamp}");
}
Console.WriteLine();
});
Console.WriteLine("Clearing topics");
topicSourceList.Clear();
Console.WriteLine("Cleared topics");
Console.ReadLine();However, the problem here is that I seem to get an update for each group cleared when executing Is it possible to get a single update when executing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The reason is you have a nested observable list and each one of these is producing an extra notification. You can achieve the desired result and simplify the code by using var latestTopicByCategoryObservable = topicSourceList.Connect()
.GroupWithImmutableState(x => x.Category)
.Transform(grouping => grouping.Items.OrderByDescending(t => t.Timestamp).First());
latestTopicByCategoryObservable.Subscribe(x =>
{
Console.WriteLine(x.TotalChanges);
});The above code produces a single change set for Clear. |
Beta Was this translation helpful? Give feedback.
The reason is you have a nested observable list and each one of these is producing an extra notification. You can achieve the desired result and simplify the code by using
GroupWithImmutableStateinstead onGroupOn. The former produces an array of items instead of a nested observable cache for each group category.The above code produces a single change set for Clear.