Created
July 17, 2017 20:09
-
-
Save ncreated/8bed7a2be50ba8b3349bf31f7ad736ae to your computer and use it in GitHub Desktop.
Medium blogpost snippet
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
| func autocomplete(text: Observable<String>) -> (result: Driver<AutocompleteResult>, isBusy: Driver<Bool>) { | |
| let provider = self.provider | |
| let isLoadingSubject = PublishSubject<Bool>() | |
| let result: Driver<AutocompleteResult> = text | |
| .do(onNext: { _ in isLoadingSubject.onNext(true) }) | |
| .flatMapLatest { query -> Observable<AutocompleteResult> in | |
| provider | |
| .autocomplete(text: query) | |
| .map { .predictions($0) } | |
| .catchError { .just(.error($0)) } | |
| } | |
| .asDriver(onErrorJustReturn: .predictions([])) | |
| .do(onNext: { _ in isLoadingSubject.onNext(false) }) | |
| let isLoading = isLoadingSubject | |
| .asDriver(onErrorJustReturn: false) | |
| return (result: result, isBusy: isLoading) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment