Skip to content

Instantly share code, notes, and snippets.

@jordanebelanger
Created June 7, 2018 20:07
Show Gist options
  • Save jordanebelanger/b58c98726369429e68996cf8f9f72bb5 to your computer and use it in GitHub Desktop.
Save jordanebelanger/b58c98726369429e68996cf8f9f72bb5 to your computer and use it in GitHub Desktop.
Generically create an RxSwift Single observable from any Alamofire's Result type based asynchronous callback function.
import RxSwift
import Alamofire
extension Single {
static func make<T>(from resultFn: @escaping (@escaping (Result<T>) -> Void) -> Void) -> Single<T> {
return Single<T>.create { sub in
resultFn { result in
switch result {
case .success(let value):
sub(SingleEvent.success(value))
case .failure(let error):
sub(SingleEvent.error(error))
}
}
return Disposables.create()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment