sergdort / CleanArchitectureRxSwift

Example of Clean Architecture of iOS app using RxSwift
MIT License
3.9k stars 496 forks source link

Issues with the current ViewModel implementation. #40

Open jdisho opened 6 years ago

jdisho commented 6 years ago

Hi Serg,

First of all, I want to thank you for the great explanation about CleanArchitecture, but I am a bit concerned about the way the ViewModel is created. Based on this approach, if a change is made in the Input, the transform function will be called and all the outputs will be recreated again. This is not necessarily right.

extension ViewModel: ViewModelType {

    struct Input {
        ...
    }

    struct Output {
        ...
    }

    func transform(input: Input) -> Output {
        ...
        return Output(output: someOutput)
    }

}

What if we create the ViewModel like this, in a protocol oriented way:

protocol ViewModelInput {
    ...
}

protocol ViewModelOutput {
    ...
}

protocol ViewModelType {
    var input: ViewModelInput { get }
    var output: ViewModelOutput { get }
}

extension ViewModel: ViewModelInput, ViewModelOutput, ViewModelType {

   var input: ViewModelInput { return self }
   var output: ViewModelOutput { return self }

   ...
}

The ViewModel is easier to be tested and the outputs aren't recreated for every change in Input.

What do you think?

jdisho commented 6 years ago

@sergdort Anything?

sergdort commented 6 years ago

Hi @jdisho

Not sure what do you mean by:

if a change is made in the Input, the transform function will be called and all the outputs will be recreated again.

I believe that in the example app Input is just a struct that contains user input observables