-
-
Save munho/1d3a6aa2787365a2375258217073bfd5 to your computer and use it in GitHub Desktop.
RxSwift callback chaining
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
func fetchUserId() -> Observable<String> { | |
return create{ (observer) -> Disposable in | |
Client.fetchUserId() { [unowned self] | |
(userId: String?, err: ErrorType?) -> Void in | |
if let _ = err{ | |
observer.on(Event.Error(err!)) | |
} else { | |
observer.on(Event.Next(userId)) | |
observer.on(Event.Completed) | |
} | |
} | |
return NopDisposable.instance | |
} | |
} | |
func fetchUserFollowersCount(userId: String) -> Observable<Int> { | |
return create{ (observer) -> Disposable in | |
Client.fetchUserFollowerCount(userId) { [unowned self] | |
(userFollowersCount: Int?, err: ErrorType?) -> Void in | |
if let _ = err{ | |
observer.on(Event.Error(err!)) | |
} else { | |
observer.on(Event.Next(userFollowersCount)) | |
observer.on(Event.Completed) | |
} | |
} | |
return NopDisposable.instance | |
} | |
} | |
// impl | |
fetchUserId().flatMap() { userId in | |
return fetchUserFollowersCount(userId) | |
} | |
.observeOn(MainScheduler.sharedInstance) | |
.subscribeNext { userFollowersCount in | |
... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment