When I was studying RxSwift and looking at the definition of the Observable
class, the protocol was too complicated (for me) and confused.
So, I would like to summarize the definition while studying the Observable
class and the protocol.
public class Observable<Element> : ObservableType {abridgement}
The following describes ObservableType
and ObservableConvertibleType
.
ObservableType
public protocol ObservableType: ObservableConvertibleType {abridgement}
A protocol that conforms to Observable
.
What is defined is ...
subscribe
is defined within the ObservableType
protocol
asObservable
is defined by the ObservableConvertibleType
protocol.
Is it like this ↓
subscribe
public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element
** Arguments **: Element
, which is the associated type of the ObserverType
protocol, and Element
, which is the associated type of the ObservableConvertibleType
protocol, are equal.
** Return value **: Value based on the Disposable
protocol
Disposable
is a protocol for unsubscribing.observable.subscribe(onNext: {
print($0)
})
When actually using it, use it for a value that has Observable
.
Print ($ 0)
is executed when an event is sent by onNext
.
Besides onNext
, there are onError
and onCompleted
.
asObservable
public func asObservable() -> Observable<Element> {
return Observable.create { o in
return self.subscribe(o)
}
}
** Arguments **: None
** Return value **: Returns a Observable
with any type Element
I want to monitor it, but it's not Observable
. At that time, if you use this method, you can treat it as Observable
.
However, it can only be used for ObservableType
.
ObserverType
public protocol ObserverType {
associatedtype Element
@available(*, deprecated, renamed: "Element")
typealias E = Element
func on(_ event: Event<Element>)
}
extension ObserverType {
public func onNext(_ element: Element) {
self.on(.next(element))
}
public func onCompleted() {
self.on(.completed)
}
public func onError(_ error: Swift.Error) {
self.on(.error(error))
}
}
Before we talk about the new ObserverType
in the subscribe
type, let's take a quick look at Event
.
public enum Event<Element> {
case next(Element)
case error(Swift.Error)
case completed
}
Looking at the definition, it looks like this,
next
notifies the Observer of a value update.error
notifies you of an error when an error occurs.completed
will notify you when the event is over.
func on(_ event: Event<Element>)
When you actually use this method, you can use either one as follows.
observer.on(.next(element))
observer.onNext(element)
ObservableConvertibleType
public protocol ObservableConvertibleType {
associatedtype Element
@available(*, deprecated, renamed: "Element")
typealias E = Element
func asObservable() -> Observable<Element>
}
Let's take a look at the ObservableConvertibleType
protocol inherited by the ObservableType
protocol, which is compliant with the Observable
class (confusing ...)
func asObservable() -> Observable<Element>
About this method See asObservable in ObservableType.
I'm glad that this protocol isn't difficult.
I wrote a lot to organize my thoughts, so it may not be easy to understand, but I personally think that I have deepened my understanding. After all, I thought it was important to jump to the definition and follow the code (although there is resistance in the brain).
Recommended Posts