Refactor MVVM with RxSwift to MVC iOS App within Minutes

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

Introducing MVC

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

--

--

--

Casual iOS developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Tutorial #2: How to view the raw HTTP traffic, parameter and data via proxy for mobile applications…

【SwiftUI】Create Paging Banner

WhatsApp to get message reactions on iPhone, Android soon: Report

Factors to consider when choosing an IOS development company

Swift Business logic component (BLoC)

Six (or So) Flavors of Dependency Injection in Swift

Compatible with Objective-C Swift Code

Finding the Ultimate Swift Code-Style Guidelines

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
Jim Lai

Jim Lai

Casual iOS developer

More from Medium

Understanding RxSwift basics in the simplest way

Reactive MVVM Pattern in UIKit

How to fail miserably with RxSwift + MVVM

Swift UIKit: How to register and dequeue reusable cells in more elegant way.