Proper usage of RxSwift to chain requests, flatMap or something else?
<p>First of all, I'm new to rxswift so I guess the answer is obvious however at the moment I can't find solution by myself.</p>

<p>I have two functions:</p>

<pre><code>func downloadAllTasks() -&gt; Observable&lt;[Task]&gt;
func getTaskDetails(taskId: Int64) -&gt; Observable&lt;TaskDetails&gt;
</code></pre>

<p>First one is downloading the list of Task objects using network request, second one downloading task details for sepcific task (using it's id)</p>

<p>What I want of achieve is to download all tasks and then for each task I want to download its details and subscribe for the event fired when all tasks details are ready.</p>

<p>So I guess I should subscribe somehow to Observable&lt;[TaskDetails]> but I don't know how to do it.</p>

<pre><code>        downloadAllTasks()
        .flatMap{
            ... // flatMap? something else?
        }
        .subscribe(
            onNext: { details in
                print("tasks details: \(details.map{$0.name})")
        })
        .addDisposableTo(disposeBag)
</code></pre>

<p>//EDIT</p>

<p>Thanks to Silvan Mosberger answer I'm much closer to the solution. One problem left. Now I have something like this:</p>

<pre><code>    downloadAllTasks()
        .flatMap{ Observable.from($0) } 
        .map{ $0.id }
        .flatMap{ [unowned self] id in
            self.getTaskDetails(taskId: id).catchError{ error in
                print("$$$ Error downloading task \(id)")
                return .empty()
            }
        }
        .do(onNext: { _ in
            print(" $$$ single task details downloaded")
        } )
        .toArray()
        .debug("$$$ task details array debug", trimOutput: false)
        .subscribe({ _ in
            print("$$$ all tasks downloaded")
        })
        .addDisposableTo(disposeBag)
</code></pre>

<p>The output is </p>

<pre><code>$$$ task details array debug -&gt; subscribed
$$$ single task details downloaded
$$$ single task details downloaded
$$$ single task details downloaded
</code></pre>

<p>There are 3 tasks available so as you can se all of them are downloaded properly however for some reason the result of toArray() - (<code>Observable&lt;[TaskDetails]&gt;</code>) doesn't produce "onNext" once all task details are ready.</p>

<p>// Edit once more</p>

<p>Ok, I'm adding simplified version of functions providing observables, maybe it will help something</p>

<pre><code>func downloadAllTasks() -&gt; Observable&lt;Task&gt; {
    return Observable.create { observer in

            //... network request to download tasks
            //...

            for task in tasks {
                observer.onNext(task)
            }
            observer.onCompleted()

        return Disposables.create()
    }
}


func getTaskDetails(id: Int64) -&gt; Observable&lt; TaskDetails &gt;  {
    return Observable.create { observer in

        //... network request to download task details
            //...

        observer.onNext(taskDetails)

        return Disposables.create()
    }
}
</code></pre>