Refactor MVVM with RxSwift to MVC iOS App within Minutes

If “so powerful” MVVM + RxSwift are, why not compare with Apple MVC?

Introducing MVC

Nothing fancy. Just follow Apple SDK, but with some refactoring in mind.

class MovieListViewViewModel {    
...

init(endpoint: Driver<Endpoint>, movieService: MovieService) {
self.movieService = movieService
endpoint
.drive(onNext: { [weak self] (endpoint) in
self
?.fetchMovies(endpoint: endpoint)
}).disposed(by: disposeBag)
}
func
viewModelForMovie(at index: Int) -> MovieViewViewModel? {
guard index < _movies.value.count else {
return nil
}
return MovieViewViewModel(movie: _movies.value[index])
}
func
fetchMovies(endpoint: Endpoint) {
self._movies.accept([])
self._isFetching.accept(true)
self._error.accept(nil)
movieService.fetchMovies(from: endpoint, params: nil,
successHandler: {[weak self] (response) in
self
?._isFetching.accept(false)
self?._movies.accept(response.results)
}) { [weak self] (error) in
self
?._isFetching.accept(false)
self?._error.accept(error.localizedDescription)
}
}
}
  1. Coupling
var json = JSON() { didSet { updateUI() }
var endpoint = Resource("URL")
...endpoint.get().onSuccess {json in self.json = json}

Conclusion

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store