0

I'm connecting to an object that asyncronously loads a collection of objects into an IEnumerable. At the time I connect, the IEnumerable may have items already in it's collection, and may add items during the lifetime of the application that I need to be notified of as they occur. As an example, it could be a bank account containing a list of bank transactions.

The challenge is this. I want to combine the processing of the initial values in the IEnumerable with any new additions. They are currently two processes. I would like to eliminate the use of NotifyCollectionChanged entirely.

I can modify the backend holding the IEnumerable. It does not need to remain as an IEnumerable if a solution to this question exists otherwise.

4

2 に答える 2

2

オブジェクトは「コールドオブザーバブル値」用であるため、IEnumerableを公開しないことをお勧めします。この場合、将来も追加のアイテムを取得できるものが必要です。

これをモデル化する最善の方法ReplaySubject<T>は、IEnumerable の代わりに使用することです。以下は、あなたと同様の状況を示す例です。

//Function to generate the subject with future values
public static ReplaySubject<int> GetSubject()
{
    var r = new ReplaySubject<int>();
    r.OnNext(1); r.OnNext(2); r.OnNext(3);
    //Task to generate future values
    Task.Factory.StartNew(() =>
    {
        while (true)
        {
            Thread.Sleep(3000);
            r.OnNext(DateTime.Now.Second);
        }
    });
    return r;
}

消費コード:

var sub = GetSubject();
sub.Subscribe(Console.WriteLine);

誰かがサブスクライブするたびsubに、これまでサブジェクトで公開されたすべての値と、このサブジェクトが将来生成する新しい値を取得します

于 2011-09-02T05:48:06.663 に答える
0

延期/再生演算子を使用できます

于 2011-10-30T15:25:17.460 に答える