2

How can I convert System.IObservable<string> to System.IObservable<System.Collections.Generic.List<string>>?

public static IObservable<List<string>> GetUrlList(Uri url) {
  var result = (from request in Observable.Return(GetWebRequest(url, false))
    from response in Observable.FromAsyncPattern<WebResponse>(
    request.BeginGetResponse, request.EndGetResponse)()
      from item in GetUrlCollection(response).ToObservable()
    select item);
  return result;
}

Here the type of the item is "list of string". I need to convert that to IObservable List of strings. How can I achieve this?

4

2 回答 2

2

Try this:

public static IObservable<List<string>> GetUrlList(Uri url)
{
    var result = (
        from request in Observable.Return(
            GetWebRequest(url, false))
        from response in Observable.FromAsyncPattern<WebResponse>(
            request.BeginGetResponse, request.EndGetResponse)()
        from item in GetUrlCollection(response)
            .ToObservable()
            .ToArray()
        select item
            .ToList());
    return result;
}

My only concern with this whole approach is that your GetUrlCollection(response) is returning an enumerable. You really should code this to return an observable.

于 2012-07-18T08:07:55.070 回答
1

Hmmm I think Observable.Start is your friend here. You have a bunch of code that it looks like you are forcing into Observable Sequences, when they really don't look like they are.

Remember Rx is designed to work with sequences of push data. You seem to have a sequence of 1 that happens to be a List. TPL/Task/async would be a good fit here.

If you do want to use Rx, I would suggest avoiding boucing around between IEnumable and IObservable. Doing so is a quick way to creating nasty race conditions and confusing the next developer.

public static IObservable<List<string>> GetUrlList(Uri url) 
{
  return Observable.Start(()=>
  {
    var request = GetWebRequest(url, false);
    return GetUrlCollection(request);//Code change here??
  });
}

Here you can happily be synchronous in your Observable.Start delegate. This should be a lot easier for the next guy to understand (i.e. this is a single value sequence with the UrlCollection as the value).

于 2012-07-25T11:28:09.503 回答