[RxSwift] I want to deepen my understanding by following the definition of Observable


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.

Observable class

public class Observable<Element> : ObservableType {abridgement}

The following describes ObservableType and ObservableConvertibleType.


public protocol ObservableType: ObservableConvertibleType {abridgement}

A protocol that conforms to Observable. What is defined is ...

  1. subscribe (method)

subscribe is defined within the ObservableType protocol asObservable is defined by the ObservableConvertibleType protocol.

Is it like this ↓ Blank-Template-Wallpaper.jpg


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

observable.subscribe(onNext: {

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.


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.


public protocol ObserverType {
    associatedtype Element

    @available(*, deprecated, renamed: "Element")
    typealias E = Element

    func on(_ event: Event<Element>)
extension ObserverType {
    public func onNext(_ element: Element) {
    public func onCompleted() {
    public func onError(_ error: Swift.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,

func on(_ event: Event<Element>)

When you actually use this method, you can use either one as follows.



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.

At the end

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

[RxSwift] I want to deepen my understanding by following the definition of Observable
I tried to deepen my understanding of object orientation by n%
I want to limit the input by narrowing the range of numbers
I want to output the day of the week
I want to var_dump the contents of the intent
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to put the JDK on my Mac PC
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
I want to control the default error message of Spring Boot
I want to change the value of Attribute in Selenium of Ruby
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.
I want to display the number of orders for today using datetime.
I want to know the JSP of the open portlet when developing Liferay
The process of understanding Gemfile by non-engineers
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
Deepened my understanding of the merge method
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five
I want to recursively get the superclass and interface of a certain class
I want to sort by tab delimited by ruby
I want to truncate after the decimal point
I want to delete files managed by Git
I want to get the value in Ruby
[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.
[Ruby] I want to make a program that displays today's day of the week!
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
I want to go back to a specific VC by tapping the back button on the NavigationBar!
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Rails] I want to display the link destination of link_to in a separate tab
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
I want to call a method of another class
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I want to judge the range using the monthly degree
I tried to reduce the capacity of Spring Boot
I want to dark mode with the SWT app
I want to stop snake case in table definition
I want to call the main method using reflection