快速执行且线程安全的可观察集合
ObservableCollection
会针对对其执行的每个操作发出通知。首先,它们没有批量添加或删除调用,其次它们不是线程安全的。
这不是让他们变慢吗?我们不能有更快的替代方案吗?有人说包裹着 ObservableCollection
的 ICollectionView
速度很快?这种说法的真实性如何。
ObservableCollection
s raise notifications for each action performed on them. Firstly they dont have bulk add or remove calls, secondly they are not thread safe.
Doesn't this make them slower? Cant we have a faster alternative? Some say ICollectionView
wrapped around an ObservableCollection
is fast? How true is this claim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果愿意的话,
ObservableCollection
可以很快。 :-)下面的代码是线程安全、更快的可观察集合的一个非常好的示例,您可以根据自己的意愿进一步扩展它。
此外,位于
ObservableCollection
之上的ICollectionView
会主动感知更改,并与任何其他源列表相比相对较快地执行过滤、分组和排序。同样,可观察集合可能不是更快数据更新的完美答案,但它们的工作做得很好。
ObservableCollection
can be fast, if it wants to. :-)The code below is a very good example of a thread safe, faster observable collection and you can extend it further to your wish.
Also
ICollectionView
that sits above theObservableCollection
is actively aware of the changes and performs filtering, grouping, sorting relatively fast as compared to any other source list.Again observable collections may not be a perfect answer for faster data updates but they do their job pretty well.
这是我制作的一些解决方案的汇编。收集的想法改变了第一个答案中的调用。
似乎“重置”操作应该与主线程同步,否则 CollectionView 和 CollectionViewSource 会发生奇怪的事情。
我认为这是因为在“重置”处理程序中尝试立即读取集合内容,并且它们应该已经就位。
如果您执行“重置”异步操作,然后立即添加一些异步项目,则新添加的项目可能会添加两次。
Here is a compilation of some solutions which I made. The idea of collection changed invokation taken from first answer.
Also seems that "Reset" operation should be synchronous with main thread otherwise strange things happen to CollectionView and CollectionViewSource.
I think that's because on "Reset" handler tries to read the collection contents immediately and they should be already in place.
If you do "Reset" async and than immediately add some items also async than newly added items might be added twice.
我无法添加评论,因为我还不够酷,但分享我遇到的这个问题可能值得发布,即使它不是真正的答案。由于 BeginInvoke,我使用此 FastObservableCollection 不断收到“索引超出范围”异常。显然,通知的更改可以在调用处理程序之前撤消,因此为了解决此问题,我传递了以下内容作为从 OnCollectionChanged 方法调用的 BeginInvoke 的第四个参数(而不是使用事件参数之一):
而不是这个:
此修复我遇到的“索引超出范围”问题。这是更详细的解释/代码 snpipet:我在哪里获得线程-安全的 CollectionView?
I can't add comments because I'm not cool enough yet, but sharing this issue I ran into is probably worth posting even though it's not really an answer. I kept getting an "Index was out of range" exception using this FastObservableCollection, because of the BeginInvoke. Apparently changes being notified can be undone before the handler is called, so to fix this I passed the following as the fourth parameter for the BeginInvoke called from the OnCollectionChanged method (as opposed to using the event args one):
Instead of this:
This fixed the "Index was out of range" issue I was running into. Here's a more detailed explaination / code snpipet: Where do I get a thread-safe CollectionView?
创建同步 Observable 列表的示例:
An example where is created a synchronized Observable list: