如何根据每个元素的条件循环可观察序列?

时间:2016-05-06 20:44:08

标签: c# while-loop system.reactive observablecollection observable

我有一个序列,我需要根据每个元素的条件重复它。例如,如果元素标有“失败”标志,我需要重新处理它。我的问题是我找不到如何进行while-loop操作。

TakeWhile几乎是我所需要的,但它不会重演。

/*
* The following lines are just an example to comprehend the idea
*/
var observableSequence = sequence.ToObservable();
observableSequence
    //This ´DoWhile´ did not worked because does not accept each element as argument
    //and sequence at this point is not the same as `observableSequence`
    .DoWhile(() => sequence.Any(item => !item.Failed))
    .Where(item => item.Failed == true) //OK here i could put another condition for limited retries...
    .Subscribe(item => {
        try{
            //Do stuff...
            //. . .
            item.Failed = false;
        } catch
        {
            item.Failed = true;
        }
    });

2 个答案:

答案 0 :(得分:2)

我建议将原始序列与一个新的observable合并,在它们失败时将它们输入。

var retries = new ReplaySubject<Foo>();
var loopSequence = sequence.ToObservable().Merge(retries);

loopSequence
    .Where(item => item.Failed)
    .Subscribe(item =>
    {
        try{
            //Do stuff
            item.Failed = false;
        } catch
        {
            item.Failed = true;
        }
        retries.OnNext(item);
    });

通常认为改变可观察对象中的对象状态是不好的做法,因此您可能需要考虑创建变换:

loopSequence
    .Where(item => item.Failed)
    .Subscribe(item =>
    {
        try{
            //Do stuff
            retries.OnNext(new Item { ..., Failed = false });
        } catch
        {
            retries.OnNext(new Item { ..., Failed = true });
        }
    });

你应该对这种模式非常小心,因为一个连续失败的项目会使你的程序执行成为一种无限循环。

答案 1 :(得分:1)

您可以包装每个值并独立重试操作:

observableSequence
  .SelectMany(item => 
    Observable.Return(item)
      .Select(x => //Do Stuff)
      //Optional argument, omitting retries infinitely until
      //success
      .Retry(3)
  )

  .Subscribe(item => {
    //Handle the results
  });

可选地,ReturnIScheduler作为其第二个参数,这可能会更改重试处理的顺序(递归与蹦床)。