Skip to content

Instantly share code, notes, and snippets.

@M0rtyMerr
Last active June 2, 2019 18:24
Show Gist options
  • Save M0rtyMerr/2ff887af6e16f768ce0cd909f731d86a to your computer and use it in GitHub Desktop.
Save M0rtyMerr/2ff887af6e16f768ce0cd909f731d86a to your computer and use it in GitHub Desktop.
Small useful operators from RxSwiftExt, my top-3.
//______ unwrap
Observable.from([1, 2, nil, 4])
.unwrap()
.bind { print($0) }
// Output: 1, 2, 4
//______ filterMap
Observable.of(1, 2, 3, 4, 5, 6)
.filterMap { $0 % 2 == 0 ? .map($0 * 2) : .ignore }
// Output: 4, 8, 12
//______ apply
func requestPolicy(_ request: Single<Response>) -> Single<Response> {
return request.retry(maxAttempts)
.do(onNext: sideEffect)
.map { Response.success }
.catchError { error in Observable.just(parseRequestError(error: error)) }
}
let resilientRequest = request.apply(requestPolicy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment