My RxSwift Summary ③ (What is Observable?)

This article is from My RxSwift Summary ①, My RxSwift Summary② It is a continuation.

What is an Observable?

ʻObservable, ʻobservable sequence, sequence, stream These are all words that have the same meaning and are ** event flow **. (In RxSwift, sequence is often used)

image.png (Quote)

*** Everything is a sequence ***.

Events here include numbers, instances of classes defined by themselves, tap gestures, and so on.

This event is defined by the following ʻEnum`.

Event.swift


public enum Event<Element> {
    case next(Element)
    case error(Error)
    case completed
}

What happens in each case, ʻObservable (sequence)If you look at it with the marble diagram that shows below, ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/595260/cba274d6-319e-2902-494c-adc3ce7d3cf7.png) In the case ofnext, ʻObservable emits the next event. It also keeps emitting events until ʻerror / completed` is called.

image.png In case of ʻerror, ʻError is emitted, and ʻobservable` stops the emission of events.

image.png In the case of completed, ʻobservable` is stopped and the event is stopped.

How to process using Observable?

ʻIn order to use the events emitted from Observable, you need to subscribe. (ʻObservable does not emit an event or process with ʻoperator until it is Subscribe) At this time, handlercan be added for each event type. Also, if it isnext, the error instance can be passed to handler if it is ʻElement, and if it is ʻerror`.

Example

Consider the Observable shown in the marble diagram below

image.png

Example.swift


let one = "1"
let two = "2"
let three = "3"

let observable = Observable.of(one, two, three)
observable.subscribe { event in
  print(event)
}

output


next(1)
next(2)
next(3)
completed

If you subscribe, you will be able to handle events in sequence. When you print it, next flows 3 times like the output, and finally completed which means stop of ʻobservable` flows.

Recommended Posts

My RxSwift Summary ③ (What is Observable?)
My RxSwift Summary ⑤ (What is Subjects / Relay?)
My RxSwift Summary ④ (What is dispose bag?)
My RxSwift Summary ②
My RxSwift Summary ①
What is Java Assertion? Summary.
What is Cubby
What is Docker?
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??