Nested observable freezes in idle mode ()

In a C # console application, using System.Reactive.Linq, I try to make observable, where each element is the string result of some processing by another observable. I created a simple image using strings and characters. Warning, this example is completely CONTROL, and the fact is that the nested .Wait () hangs.

class Program
{
    static void Main(string[] args)
    {
        string[] fileNames = { "file1.doxc", "file2.xlsx", "file3.pptx" };
        IObservable<string> files = fileNames.ToObservable();
        string[] extensions = files.Select(fn =>
        {
            var extension = fn.ToObservable()
            .TakeLast(4)
            .ToArray()
            .Wait(); // <<<<<<<<<<<<< HANG HERE
            return new string(extension);
        })
        .ToArray()
        .Wait();
    }
}

Again, this is not how I find the suffix of many file names. The question is, how can I create an Observable of strings where strings are computed from a completed observable.

If I pulled out this code and ran it alone, it will complete perfectly.

     var extension = fn.ToObservable()
        .TakeLast(4)
        .ToArray()
        .Wait();

There is something about the nested Wait () for async methods that I don't understand.

How can I encode nested async observables, so I can create a simple array of strings?

thank

-John

+4
3

, , , ToObservable() . CurrentThreadScheduler.

, files OnNext() [A] ( "file1.doxc") . , OnNext(). fn ToObservable(), Wait() , fn - OnNext() ( "f") , , OnNext() [A] .

:

files :

IObservable<string> files = fileNames.ToObservable(NewThreadScheduler.Default);

, Wait() SelectMany ( Rx):

string[] extensions = files.SelectMany(fn =>
{
    return fn.ToObservable()
             .TakeLast(4)
             .ToArray()
             .Select(x => new string(x));
})
.ToArray()
.Wait();

// display results etc.

- . , Wait(). Spy, ToObservable(), .

+5

Wait - , Rx. , .

, :

IObservable<string> files = fileNames.ToObservable();
string[] extensions = await files.SelectMany(async fn =>
{
    var extension = await fn.ToObservable()
    .TakeLast(4)
    .ToArray();
        return new string(extension);
})
.ToArray();
+1

, , :

    string[] fileNames = { "file1.doxc", "file2.xlsx", "file3.pptx" };
    string[] extensions =
    (
        from fn in fileNames.ToObservable()
        from extension in fn.ToObservable().TakeLast(4).ToArray()
        select new string(extension)
    )
        .ToArray()
        .Wait();

.Wait(). - :

    IDisposable subscription =
    (
        from fn in fileNames.ToObservable()
        from extension in fn.ToObservable().TakeLast(4).ToArray()
        select new string(extension)
    )
        .ToArray()
        .Subscribe(extensions =>
        {
            /* Do something with the `extensions` */
        });

.

+1
source

Source: https://habr.com/ru/post/1684955/


All Articles