Skip to content

Instantly share code, notes, and snippets.

@ppth0608
Created July 14, 2019 05:20
Show Gist options
  • Save ppth0608/8acc2f61ba1ae898af1c805f1d442ca0 to your computer and use it in GitHub Desktop.
Save ppth0608/8acc2f61ba1ae898af1c805f1d442ca0 to your computer and use it in GitHub Desktop.
How use Notification in RxSwift
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)
@philosopherdog
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment