Managing subscriptions for Reactive + MVVM architecture


(NOTE: For this article will be using iOS in the examples, but is applicable to all reactive MVVM architectures. Will also be using google style guidelines to format the constructs)

Just want to share an idea when we are implementing MVVM pattern with reactive programming such as RxSwift/RxJava, etc.

Our team is going about implementing the MVVM pattern with RX described in article

The gist of it is defining inputs/outputs between View ViewModel.


Our team lead decided that theprocess of managing reactive subscriptions (DisposeBag for Rx, Cancellables for iOS Combine) should be managed by the owner of the view model; the view (UIViewController, UIView in case of iOS).

This caused us to create a property in the Output for every corresponding input [TAG#1] and and managing the subscription at the call site ([TAG#2]).

For this, I suggest we store the cancellables in a array of cancellables and pass it as a single param back, instead of a subscription for each input:

…and handle it at the call site (ViewController here) like so:

What if we are handling the inputs with side effects using the handleEvents? Doing so returns a publisher instead of cancellables.

For this we can leverage merging of publishers (Merge operator in case of combine) like so:

Thanks for reading!