Last active
February 23, 2022 06:03
-
-
Save saiday/88530a3575a9d3f01e4d to your computer and use it in GitHub Desktop.
RxSwift callback chaining
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 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
What does
create
look like?