RxSwift: Create Operator
- Specifies all events that an observable will emit to subscribers.
- Takes a single parameter named
subscribe. - Provide the implementation of calling
subscribeon the Observable. subscribeparameter is an escaping closure that takes anAnyObserverand returns aDisposable.AnyObserveris a generic type that allows you to add valuesontoan observable sequence, which will then be emitted to subscribers.- Most flexible way to create a custom observable however, you have to remember to send a
.completedevent and also returnDisposable.
Example:
Observable<String>.create { observer in
observer.onNext("Gary Hooper")
observer.onNext("Tony Stokes")
observer.onCompleted()
// No point in .onNext again as we have completed
// and no more events will be emitted.
return Disposables.create()
}
< All Posts