使用Rx.Net搜索实现

时间:2016-05-12 09:45:54

标签: c# generics system.reactive reactive-programming tpl-dataflow

我正在使用C#中的Rx创建搜索页面实现。我创建了一个通用搜索方法来搜索关键字并在UI上添加结果列表。以下是代码:

通用搜索方法:

   public static IObservable<TResult> GetSearchObservable<TInput, TResult>(INotifyPropertyChanged propertyChanged,
                string propertyName, TInput propertyValue, Func<TInput, TResult> searchFunc)
            {
                // Text change event stream
                var searchTextChanged = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
                                                  ev => propertyChanged.PropertyChanged += ev,
                                                  ev => propertyChanged.PropertyChanged -= ev
                                                  )
                                                  .Where(ev => ev.EventArgs.PropertyName == propertyName);

                // Transform the event stream into a stream of strings (the input values)
                var inputStringStream = searchTextChanged.Throttle(TimeSpan.FromMilliseconds(500))
                                                         .Select(arg => propertyValue);

                // Setup an Observer for the search operation
                var search = Observable.ToAsync<TInput, TResult>(searchFunc);

                // Chain the input event stream and the search stream, cancelling searches when input is received
                var results = from searchTerm in inputStringStream
                              from result in search(propertyValue).TakeUntil(inputStringStream)
                              select result;

                return results;
            }

使用搜索通用方法:

  var rand = new Random();
            SearchObservable.GetSearchObservable<string, string>(this, "SearchString", SearchString, search =>
            {
                Task.WaitAll(Task.Delay(500));

                return SearchString;
            })
            .ObserveOnDispatcher()
            .Subscribe(rese => 
            {
                LstItems.Clear();

                LstItems.Add(SearchString);

                // Heavy operation lots of item to add to UI
                for(int i = 0; i < 200000; i++)
                {
                    LstItems.Add(rand.Next(100, 100000).ToString());
                }

                Result = rese;
            });

此代码中有2个问题需要帮助来解决这些问题:

  1. 在泛型方法行from result in search(propertyValue).TakeUntil(inputStringStream)中传递的搜索关键字值始终为null,因为propertyValue是字符串类型,因此作为值传递给方法。如何在搜索方法中发送更新的值?

  2. 在订阅方法上执行繁重的UI操作时,如添加大量随机数的示例。重复执行搜索时会出现此问题。最终UI阻止。我该如何解决这个问题?

  3. 对于第二点是否有任何方法可以取消之前的UI操作并运行一个新操作。这只是一个想法。但需要一些建议来解决这些问题。

1 个答案:

答案 0 :(得分:0)

  1. 您可以传递一个读取属性值的函数,即代替TInput propertyValue,您的方法应该接受Func<TInput> propertyValueGetter
  2. 您也可以使用ReactiveUI库。在这种情况下,您的代码看起来像这样:

    this.WhenAnyValue(vm => vm.SearchString)
        .Throttle(TimeSpan.FromMilliseconds(500))
        .InvokeCommand(DoSearchString);
    
    DoSearchString = ReactiveCommand.CreateAsyncTask(_ => {
       return searchService.Search(SearchString);
    });
    
    DoSearchString.ObserveOn(RxApp.MainThreadScheduler)
                  .Subscribe(result => {
                      LstItems.Add(result);
                  })
    
    1. 我不相信这个问题有一个普遍的答案。尽可能在TaskPool上做,并在准备好显示数据时切换到带有ObserveOn的UI线程。在您的情况下,您可以按批次插入项目,而不是一次性插入。