RxSwift’s Many Faces of FlatMap

The Value of flatMap

let string = Observable.of("a", "b", "c")
string
.flatMap { Observable.of("\($0)-x", "\($0)-y", "\($0)-z") }
.subscribe(onNext: { element in
print(element) // this prints 9 values.
})
string
.flatMap { URLSession.shared.rx.data(request: URLRequest(url: URL(string: "http://myserver.com/api/get_user?id=\($0)")!)) }
.subscribe(onNext: { element in
print(element) // this prints the result of the 3 network requests.
})

The Choices and Their Differences

Summary

--

--

I started programming as a hobby in the late ’70s and professionally in the late ’90s. I’ve been writing iOS apps since 2010 and using RxSwift since 2015.

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

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