Created
June 7, 2018 20:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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