RxSwift: The Complexity Tradeoff

Simple Examples vs Complex Examples

Github Search Example

The “Less Than Simple” code

class CompactMapErrorViewModel {

var data: Observable<[String]>!
var error: Observable<String?>!
var dataService = DataService() init(load: Observable<Void>) {
let loading = load
.flatMapLatest { [unowned self] _ in
self.dataService.load()
.retry(2)
.timeout(.seconds(3), scheduler: MainScheduler.instance)
.materialize()
}
.observeOn(MainScheduler.instance)
.share()
data = loading
.compactMap { $0.element }
error = loading
.compactMap { $0.error?.localizedDescription }
}
}

Automatic Error Retry

Automatic Timeout

Requirements

So, complex… or not?

Michael Long is a Senior Lead iOS engineer at CRi Solutions, a leader in cutting edge iOS, Android, and mobile corporate and financial applications.

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