매거진 ReactiveX

You can make anything
by writing

C.S.Lewis

by Tilltue Jul 10. 2016

RxSwift, 블랙잭 용어 알아보기

Publish, Behavior, Replay 블랙잭 용어.

publish블랙잭 용어

* 이 포스트는 RxSwift 4.3.1, swift 4.2 버전을 기준으로 작성되었습니다.

* RxSwift 의 Publish블랙잭 용어.swift, Behavior블랙잭 용어.swift, Replay블랙잭 용어.swift 내용


ReactiveX Observable 에는 "Hot Observable" 과 "Cold Observable"의 개념이 있는데, 블랙잭 용어 는 Cold Observable을 Hot 하게 변형하는 효과를 얻을 수 있다.

블랙잭 용어 는 Imperative eventing어떤 이벤트를 발생 하고 싶을때. 얼마나 많은 객체에게 그 이벤트을 구독 하는지 중요하지 않다. 원하는 이벤트를 subscription ( observer ) 존재 여부와 관계없이 이벤트를 발행 할 수 있다.


4가지 종류의 블랙잭 용어 가 있다.



1. Async블랙잭 용어

Complete 될때까지 이벤트는 발생되지 않으며, complete 가 되면 마지막 이벤트를 발생하고 종료된다.


클래스 선언부

class Async블랙잭 용어<Element

: Observable<Element

, 블랙잭 용어Type

, ObserverType

, SynchronizedUnsubscribeType


블랙잭 용어

만약 에러로 종료되면 마지막 이벤트 전달 없이 에러가 발생됩니다.

블랙잭 용어


예제

let async블랙잭 용어 = Async블랙잭 용어<String()

async블랙잭 용어.debug().subscribe{print($0)}.disposed(by: disposeBag)

async블랙잭 용어.on(.next("1"))

async블랙잭 용어.on(.next("2"))

async블랙잭 용어.on(.next("3"))

async블랙잭 용어.on(.completed)


결과

(async블랙잭 용어()) - subscribed

(asyncSubject()) - Event next(3)

next(3)

(asyncSubject()) - Event completed

completed

(async블랙잭 용어()) - isDisposed


블랙잭 용어의 debug 로그는 빨간색, subscribe 로그는 녹색이다.

"3" 이벤트와 완료 이벤트가 전달된 것을 확인할수 있다.


2. Publish블랙잭 용어

Publish블랙잭 용어 는 subscribe 된 시점 이후부터 발생한 이벤트를 전달한다.

subscribe 되기 이전의 이벤트는 전달하지 않는다.


클래스 선언부

class Publish블랙잭 용어<Element

: Observable<Element

, 블랙잭 용어Type

, Cancelable

, ObserverType

, SynchronizedUnsubscribeType


블랙잭 용어

에러가 발생하면 마찬가지로 에러를 전달한다.


예제

let publish블랙잭 용어 = Publish블랙잭 용어<String()

publish블랙잭 용어.debug().subscribe{print("first subscribe :\($0)")}.disposed(by: disposeBag)


publish블랙잭 용어.on(.next("1"))

publish블랙잭 용어.on(.next("2"))

publish블랙잭 용어.debug().subscribe{print("second subscribe :\($0)")}.disposed(by: disposeBag)

publish블랙잭 용어.on(.next("3"))

publish블랙잭 용어.on(.completed)



결과

(publish블랙잭 용어()) - subscribed

(publish블랙잭 용어()) - Event next(1)

first subscribe : next(1)

(publish블랙잭 용어()) - Event next(2)

first subscribe : next(2)

(publish블랙잭 용어()) - subscribed

(publish블랙잭 용어()) - Event next(3)

first subscribe : next(3)

second subscribe : next(3)

(publish블랙잭 용어()) - Event completed

first subscribe :completed

(publish블랙잭 용어()) - isDisposed

(publish블랙잭 용어()) - Event completed

second subscribe :completed

(publish블랙잭 용어()) - isDisposed


블랙잭 용어의 debug 로그는 빨간색, subscribe 로그는 녹색이다.

첫번째 subscribe는 이벤트를 발생하기 전부터 구독했고, 두번째 subscribe는 두번째 이벤트 발생이후에 구독했다.

로그를 보면 첫번째는 모든 이벤트, 두번째에는 "3" 만이 전달된 것을 확인할수있다.

이처럼 포인트는 구독이후에 발생하는 이벤트를 받게 되는것


3. Behavior블랙잭 용어

기본적으로는 publish 와 크게 다르지 않지만 초기 값을 가진 subject 이다. subscribe 가 발생하면 즉시 현재 저장된 값을 이벤트로 전달한다. 마지막 이벤트 값을 저장하고 싶을때 사용한다.


클래스 선언부

class Behavior블랙잭 용어<Element

: Observable<Element

, 블랙잭 용어Type

, ObserverType

, SynchronizedUnsubscribeType

, Disposable


에러가 발생시 마찬가지로 에러를 전달한다.



let behavior블랙잭 용어 = Behavior블랙잭 용어<String(value: "tom")

behavior블랙잭 용어.debug("behavior 블랙잭 용어 log 1: ").subscribe{print($0)}.disposed(by: disposeBag)

behavior블랙잭 용어.on(.next("jack"))

behavior블랙잭 용어.on(.next("wade"))

behavior블랙잭 용어.debug("behavior 블랙잭 용어 log 2: ").subscribe{print($0)}.disposed(by: disposeBag)


결과

behavior 블랙잭 용어 log 1: - subscribed

behavior subject log 1: - Event next(tom)

next(tom)

behavior subject log 1: - Event next(jack)

next(jack)

behavior subject log 1: - Event next(wade)

next(wade)

behavior 블랙잭 용어 log 2: - subscribed

behavior subject log 2: - Event next(wade)

next(wade)


첫번째 subscribe 직후 최초 생성시 설정한 값인 tom 이 이벤트로 전달됐다.

이후 jack wade 가 전달되었으며, 이 이후 두번째 subscribe 되자, 마지막 값인 wade 가 이벤트로 전달되었다.


포인트는 마지막 이벤트의 값이 저장된다는 것이다.

마지막 값이 중요하거나, 최초 subscribe 시 이벤트가 바로 전달되어야 할때 사용하면 유리하다


4. Replay블랙잭 용어

n개의 이벤트를 저장하고 subscribe 가 되는 시점과 상관없이 저장된 모든 이벤트를 전달한다.

RxSwift 에서는 create(bufferSize bufferSize: Int) 와 createUnbounded 의 생성함수를 가진다.

createUnbounded 는 블랙잭 용어 의 생성 이후 발생하는 모든 이벤트를 저장한다.


클래스 선언부 및 생성자

class Replay블랙잭 용어<Element

: Observable<Element

, 블랙잭 용어Type

, ObserverType

, Disposable


static func create(bufferSize: Int) - Replay블랙잭 용어<Element

static func createUnbounded()- Replay블랙잭 용어<Element


let replay블랙잭 용어 = Replay블랙잭 용어<String.create(bufferSize: 2)

replay블랙잭 용어.on(.next("tom"))

replay블랙잭 용어.on(.next("jack"))

replay블랙잭 용어.on(.next("wade"))

replay블랙잭 용어.debug("replay 블랙잭 용어 log: ").subscribe{print($0)}.disposed(by: disposeBag)


결과

replay 블랙잭 용어 log: - subscribed

replay subject log: - Event next(jack)

next(jack)

replay subject log: - Event next(wade)

next(wade)


이벤트는 3번 발생했지만, 버퍼 사이즈가 2이기 때문에 jack 과 wade 만 전달되었다.


Replay블랙잭 용어 생성 부분을 아래와 같이 바꿔보자.

let replay블랙잭 용어 =ReplaySubject<String.createUnbounded()

모든 이벤트가 전달 된다.

사용할때는 메모리 관리에 유의 하자!



다음글: Combining Observables

브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari