RxSwift: Create Operator
- Specifies all events that an observable will emit to subscribers.
- Takes a single parameter named
subscribe
. - Provide the implementation of calling
subscribe
on the Observable. subscribe
parameter is an escaping closure that takes anAnyObserver
and returns aDisposable
.AnyObserver
is a generic type that allows you to add valuesonto
an 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
.completed
event 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