Created
July 14, 2019 05:20
-
-
Save ppth0608/8acc2f61ba1ae898af1c805f1d442ca0 to your computer and use it in GitHub Desktop.
How use Notification in RxSwift
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
extension ObservableType { | |
func postNotification(_ name: Notification.Name) -> Observable<E> { | |
return self.do(onNext: { element in | |
NotificationCenter.default.post(name: name, object: element) | |
}) | |
} | |
} | |
///////// | |
//Usage// | |
///////// | |
extension Notification.Name { | |
static let createCategory = Notification.Name("createCategory") | |
static let editCategorty = Notification.Name("createCategory") | |
} | |
func createCategory(with name: String) -> Observable<ClipCategory> { | |
return categoryAPI | |
.createCategory(with: name) | |
.postNotification(.createCategory) | |
} | |
NotificationCenter.default.rx.notification(.createCategory) | |
.map { _ in () } | |
.bind(to: fetchCategory) | |
.disposed(by: disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why would you post a notification and return an observable? It is already using the Observer pattern. You just need to listen with
rx.notification