0

I'm applying reactive programming to the Unity3D project. Is there a way to specify a variable number of Observable in Observable.WhenAll()?

Sample or search results suggest an explicit way to enter all non-variable items, most of which are not variable numbers.

var parallel = Observable.WhenAll(
                    ObservableWWW.Get ("http://google.com/"),
                    ObservableWWW.Get ("http://bing.com/"),
                    ObservableWWW.Get ("http://unity3d.com/");

What I want is as follows.

List<string> URLs = new List<string>();
URLs.Add("http://google.com/");
URLs.Add("http://bing.com/");
...
...
var parallel = Observable.WhenAll( ??? //ObservableWWW.Get() : count of URLs list);

Please reply. Thank you.

오명근
  • 15
  • 1
  • 4
  • The [WhenAll(this IEnumerable> sources)](https://github.com/neuecc/UniRx/blob/aa3b6d3e30354c25f5ee276d19be4f5ff7d7a82c/Assets/Plugins/UniRx/Scripts/Observable.Concatenate.cs#L260) overload already accepts an arbitrary number of observables. Perhaps you're looking for a way to *produce* a list of observables from a list of URLs? – Panagiotis Kanavos Sep 09 '19 at 10:11
  • @PanagiotisKanavos He wants a second parameter so he can say that only 2 of the Observables have to be completed like Observable.WhenSome(urlObservables, 2) – Felix Keil Sep 10 '19 at 06:24
  • @FelixKeil there are no observables in the second snippet. Even if there were, a count parameter isn't meaningful when you can limit the number of items in the `IEnumerable<>` with a `Take(2)` – Panagiotis Kanavos Sep 10 '19 at 06:59
  • @PanagiotisKanavos Take(2) selects concrete Observables out of the Enumerable. Which Observables finish shall not matter. So this is not a solution how I understood the problem. But maybe you are right. The question is unclear. – Felix Keil Sep 10 '19 at 07:15
  • @PanagiotisKanavos Seems you are right what the question is about. I have conceptual solution in my head for a WhenSome extension method, where it doesn't matter which observables are finished. – Felix Keil Sep 11 '19 at 05:24

1 Answers1

1

WhenAll(this IEnumerable> sources) already does this. I suspect the real question is how to produce some observables from a list of URLs. One way to do it would be to use LINQ :

var urlObservables=URLs.Select(url=>ObservableWWW.Get(url));

var allAsOne= Observable.WhenAll(urlObservables);

Update

As Felix Keil commented, what if the OP wants to observe only a few of those observables? That's a job for LINQ's Take(), applied either on the URL list or the observables list, eg :

var someObservables=URLs.Take(2).Select(url=>ObservableWWW.Get(url));

or even

var someObservables=URLs.Select(url=>ObservableWWW.Get(url)).Take(2);

LINQ's lazy evaluation means the operators will run only when someObservables gets enumerated. When that happens, enumeration will stop after the first two iterations so ObservableWWW.Get will be called only twice

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236