I'm looking to convert an IEnumerable collection to an IObservable one without using Rx ToObservable() and ToEnumerable() methods.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Simple answer - use
ToObservable
. That's what it's for."Answering the actual question" answer - you can avoid using
Subjects
viaObservable.Create
:Output:
If one has an
IEnumerable
of unknown type, there are two ways one can "convert it to IObservable":Copy all of the data in the object to a new collection which implements
IObservable
. If this is done, only changes made to the new collection will be reported. Changes made to the original will not.Create a new object which will periodically take snapshots of the contents of the
IEnumerable
; after taking each snapshot, report all the changes that would have to be made to the previous snapshot to make it match the new one. Using this approach, changes made to the original object will be observed, eventually, but it's hard to provide timely update notifications without wasting a lot of time repeatedly reading the collection when nothing has changed.There are some times when one needs to have the
IObservable
bound to the originalIEnumerable
object, rather than to a new object which is pre-filled with a copy of the data; in such cases, the second approach may be necessary. Often, however, it won't be possible to make the polling rate fast enough to provide timely updates without it causing unacceptable system loading unless the original collection supports features which are not available in an arbitraryIEnumerable
. Further, if one doesn't impose requirements on the type of theIEnumerable
, one will likely have to pose restrictions on the threading contexts where it might be updated.