In my pre­vi­ous post I used the IObservable<T>.Zip exten­sion method in the Drag-and-Drop exam­ple, which takes two observ­able col­lec­tions and runs a func­tion over them to return a new observ­able col­lec­tion of poten­tially a dif­fer­ent type.

But what if you just want to sim­ply merge the two observ­able col­lec­tions into one stream? Well, Rx API has another exten­sion method called Merge. To use it you can either invoke it as exten­sion method:

var zs = xs.Merge(ys);

or you can invoke it as sta­tic method on more than two observ­able collections:

var zs = Observable.Merge(xs, ys, us, vs);

To use the Merge method, all the observ­able col­lec­tions being merged need to be of the same type.

There are two things you should keep in mind:

1. OnCom­pleted is fired on the com­pos­ite observ­able col­lec­tion (zs) at the point the last OnCom­pleted is fired on any of the merged observ­able collections:

image

2. OnError is fired on the com­pos­ite observ­able col­lec­tion (zs) at the point the first OnError is fired on any of the merged observ­able col­lec­tions, any sub­se­quent val­ues on the merged observ­able col­lec­tion will not make it onto the com­pos­ite collection:

image

Share