Skip to content

Instantly share code, notes, and snippets.

@munho
Forked from saiday/RxSwiftExample.swift
Created February 23, 2022 06:03
Show Gist options
  • Save munho/1d3a6aa2787365a2375258217073bfd5 to your computer and use it in GitHub Desktop.
Save munho/1d3a6aa2787365a2375258217073bfd5 to your computer and use it in GitHub Desktop.
RxSwift callback chaining
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