Last active
October 2, 2018 17:36
-
-
Save Athosone/934b0dacafc71727d50940d9516505c4 to your computer and use it in GitHub Desktop.
RxSwift+fromAsync example
This file contains hidden or 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
// Consider this function: | |
func fetchPost(by postId: Int, completion: @escaping (Post?, Error?) -> Void) { | |
... | |
} | |
// Using our extension to use an observable | |
// fetchPostObservable looks like: fetchPostObservable(postId) -> Observable<Post> | |
let fetchPostObservable = Observable.fromAsync(postService.getPost(postId:completion:)) | |
// Now to fetch a post you just have to call it just like you would call any function which returns an Observable: | |
fecthPostObservable(10).subscribe(onNext: { (post) in | |
print("Fetching post with id 10 succeeded") | |
}, onError: { (error) in | |
print("Fetching post failed: \(error)" | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment