๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/RxSwift

[RxSwift] Subject๋ž€?

์•Œ์—‘์Šค..์˜์—…ํ•ฉ๋‹ˆ๋‹ค...๋„ค..ํ•ด์š”...

์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค : )

์ฐธ ์˜ค๋žœ๋งŒ์— RxSwift ๊ฒŒ์‹œ๊ธ€ ์“ฐ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.. ๋ฌธ ๋‹ซ์€๊ฑฐ ์•„๋‹ˆ๊ตฌ์š” ์ •์ƒ์˜์—…ํ•˜๋ ค๊ตฌ์š”.. ๋„ค..

์‚ฌ์‹ค ๊ทธ๋™์•ˆ RxSwift ๊ณต๋ถ€๋ฅผ ์•„์˜ˆ ์•ˆํ•œ ๊ฒƒ์€ ์•„๋‹Œ๋ฐ์š”.. ๋ญ”๊ฐ€ ๊ฒŒ์‹œ๊ธ€์„ ์“ธ ๋•Œ ํ™•์‹คํ•œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•œ๋‹ค๋Š” ์ƒ๊ฐ์— ์ข€ ๋” ๊ณต๋ถ€ํ•˜๊ณ  ์ •๋ฆฌํ•ด์„œ ์˜ฌ๋ฆฌ์ž ์ข€ ๋” ํ•˜๊ณ ... ํ•˜๋‹ค๊ฐ€ ์ด๋ ‡๊ฒŒ ๋ฏธ๋ฃจ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋„ค์š” ํ•˜ํ•ซ

์ด์ œ ๋” ์ด์ƒ ๋ฏธ๋ฃฐ ์ˆ˜ ์—†๋‹ค! ๋งŽ์ด ๋ถ€์กฑํ•ด๋„ ๊ธ€๋กœ ์ •๋ฆฌํ•˜๋ฉด์„œ ํ™•์‹คํžˆ ์ดํ•ดํ•˜๋Š” ๋ถ€๋ถ„๋“ค์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ , ์ตœ๊ทผ์— ํ”„๋กœ์ ํŠธ์—์„œ RxSwift๋ฅผ ์ด์šฉํ•ด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ผ์ด ๋งŽ์€๋ฐ ์ •ํ™•ํžˆ ์•Œ์ง€ ๋ชปํ•˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ๋‹ค๊ณ  ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.. ๊ทธ๋ž˜์„œ ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ์‹ถ์–ด์„œ ๋‹ค์‹œ RxSwift ๊ณต๋ถ€๋ฅผ ์—ด์‹ฌํžˆํ•˜๋ฉฐ ํ‹ฐ์Šคํ† ๋ฆฌ์—๋„ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! 

๋งˆ์ง€๋ง‰์œผ๋กœ Observable์— ๋Œ€ํ•ด ์ •๋ฆฌํ–ˆ๋”๋ผ๊ตฌ์š”.. ๊ทธ๋ž˜์„œ Subject๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ์•„์ขŒ์ขŒ!

 

์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์€ RxSwift: Reactive Programming with Swift ์ฑ…์˜ Subjects ์ฑ•ํ„ฐ์™€ ReactiveX ์˜ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด์„œ Subject์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

Subject๋ž€?

Subject๋ฅผ ํ•œ๋งˆ๋””๋กœ ์ •์˜ํ•˜์ž๋ฉด Observable์ด์ž Observer์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ๋Œ€์ฒด ๋ฌด์Šจ ๋ง์ด๋ƒ... ์ง€๊ธˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์•Œ์•„๊ฐ€๋ด…์‹œ๋‹ค.

 

Observable์€ '๊ด€์ฐฐ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„' ์ •๋„๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ์ •๊ธฐ๊ฐ„ ๋™์•ˆ ๊ณ„์†ํ•ด์„œ ์ด๋ฒคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐฉ์ถœํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹ค์ œ ์•ฑ ๊ฐœ๋ฐœ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฒƒ์€ ์‹ค์‹œ๊ฐ„์œผ๋กœ Observable์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  subscriber์—๊ฒŒ ๋ฐฉ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, observable์ด์ž observer๋กœ ๋™์ž‘ํ•˜๋Š” ๋ฌด์–ธ๊ฐ€๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ์ด ๋ฌด์–ธ๊ฐ€๊ฐ€ ๋ฐ”๋กœ Subject์ž…๋‹ˆ๋‹ค.

 

๋ณธ๊ฒฉ์ ์œผ๋กœ Subject์— ๋Œ€ํ•ด ์•Œ์•„๊ฐ€๊ธฐ์— ์•ž์„œ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ถ€ํ„ฐ ๋ณด๊ณ  ์‹œ์ž‘ํ•ด๋ด…์‹œ๋‹ค!

 example(of: "PublishSubject") {

     // 1
     let subject = PublishSubject<String>()

     // 2
     subject.onNext("Is anyone listening?")

     // 3
     let subscriptionOne = subject
         .subscribe(onNext: { (string) in
             print(string)
         })

     // 4
     subject.on(.next("1"))		// Print: 1

     // 5
     subject.onNext("2")		// Print: 2
 }

PublishSubject๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ์ธ๋ฐ์š”, ์ฃผ์„์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค.


1. PublishSubject ์ƒ์„ฑ

๋จผ์ € subject๋ผ๋Š” ์ด๋ฆ„์˜ PublishSubject ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋Š”๋ฐ์š”,

PublishSubject๋Š” ์ด๋ฆ„์—์„œ๋„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋“ฏ์ด ์‹ ๋ฌธ ์ถœํŒ์‚ฌ์ฒ˜๋Ÿผ ์ •๋ณด๋ฅผ ๋ฐ›์•„ ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ๋ฐฐํฌํ•ฉ๋‹ˆ๋‹ค. (PublishSubject์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋” ์ž์„ธํžˆ ์ •๋ฆฌํ• ๊ฒŒ์š”!) ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” String ํƒ€์ž…์œผ๋กœ ์ƒ์„ฑํ–ˆ์œผ๋‹ˆ String ํ˜•์˜ ์ •๋ณด๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  ๋˜ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ ? 

๊ฐ์ฒด๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์—ˆ๋‹ค๋ฉด ์ด์ œ ์ •๋ณด๋ฅผ ๋ฐ›์„ ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

2~3. ๊ตฌ๋…์„ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด ๊ฐ’์€ ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค

2๋ฒˆ ์ฝ”๋“œ์—์„œ subject์— ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ฐ’์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ์ฝ”๋“œ๋Š” ํ”„๋ฆฐํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์™œ๋ƒํ•˜๋ฉด ์•„์ง observer๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ 3๋ฒˆ์—์„œ subject๋ฅผ subscribe ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์™œ ๊ตฌ๋…์„ ํ–ˆ๋Š”๋ฐ๋„ ์ถœ๋ ฅ์ด ๋˜์ง€ ์•Š๋ƒ๋ฉด! PublishSubject๋Š” ์˜ค์ง ํ˜„์žฌ์˜ subscriber์—๊ฒŒ๋งŒ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒคํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ ๊ตฌ๋…์„ ํ•˜์ง€ ์•Š๊ณ  ์žˆ์—ˆ๋‹ค๋ฉด ๊ตฌ๋…ํ•œ ์ดํ›„์—๋„ ๊ทธ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

 

4~5. ๊ตฌ๋… ์ดํ›„ ์ถ”๊ฐ€๋œ ๊ฐ’ ๋‚ด๋ณด๋‚ด๊ธฐ

์ด์ œ ๊ตฌ๋…์„ ํ–ˆ์œผ๋‹ˆ ์ดํ›„์— ์ถ”๊ฐ€๋˜๋Š” ๊ฐ’๋“ค์€ ๋ชจ๋‘ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒ ์ฃ ??

๋‹น์—ฐํ•œ ์ด์•ผ๊ธฐ๊ฒ ์ง€๋งŒ subject๋ฅผ String ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€๋˜๋Š” next ์ด๋ฒคํŠธ ๋‚ด์˜ ๊ฐ’์€ String ํƒ€์ž…์ด์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค! .on(.next(_:))๋Š” subscribe ์—ฐ์‚ฐ์ž์™€ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ƒˆ๋กœ์šด next ์ด๋ฒคํŠธ๋ฅผ subject์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ด๋ฒคํŠธ ๋‚ด์˜ ๊ฐ’๋“ค์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ†ต๊ณผ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์ž๋„ subscribe์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ถ•์•ฝํ˜•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 5๋ฒˆ ์ฝ”๋“œ์™€ ๊ฐ™์ด .onNext(_:)๋กœ ์ถ•์•ฝํ•ด์„œ ํ›จ์”ฌ ๋” ๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ ์ถ”๊ฐ€๋œ ๊ฐ’์ด ๋ชจ๋‘ ํ”„๋ฆฐํŠธ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Subject์˜ ์ข…๋ฅ˜

Subject๋Š” observable์ธ ๋™์‹œ์— observer์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์œ„์—์„œ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ subject๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š”์ง€(observer์˜ ์—ญํ• ), ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ป๊ฒŒ ๊ตฌ๋…๋˜๋Š”์ง€(observable์˜ ์—ญํ• )๋ฅผ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. next ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ด๊ฒƒ์„ ๋‹ค์‹œ subscriber์—๊ฒŒ ๋ฐฉ์ถœํ•˜๋Š” ๊ณผ์ •์„ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ subject์— ๋Œ€ํ•ด ์กฐ๊ธˆ ์ดํ•ด๊ฐ€ ๊ฐ€์‹œ๋‚˜์š”??? ์ด์ œ Subject์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

RxSwift์—๋Š” 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ Subject๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • PublishSubject: ๋นˆ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ’๋งŒ์„ subscriber์— ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค. 
  • BehaviorSubject: ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ€์ง„ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•˜์—ฌ, ์ƒˆ๋กœ์šด subscriber์—๊ฒŒ ์ดˆ๊ธฐ๊ฐ’ ๋˜๋Š” ์ตœ์‹ ๊ฐ’์„ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • ReplaySubject: ์ •ํ•ด์ง„ ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฐ€์ง„ ์ฑ„๋กœ ์ดˆ๊ธฐํ™”๋˜๊ณ  ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ ๋งŒํผ์˜ ๊ฐ’๋“ค์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด subscriber์—๊ฒŒ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค. 
  • AsyncSubject: subject๊ฐ€ completed ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ๋•Œ๋งŒ ์‹œํ€€์Šค์˜ ๋งˆ์ง€๋ง‰ next ์ด๋ฒคํŠธ๋งŒ์„ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค. (๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” Subject์ด๊ธฐ ๋•Œ๋ฌธ์— raywenderich ์ฑ…์—์„œ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค)

ํ•˜๋‚˜์”ฉ ์ž์„ธํžˆ ์‚ดํŽด๋ด…์‹œ๋‹ค!

PublishSubject

PublishSubject๋Š” ๊ตฌ๋…๋œ ์ˆœ๊ฐ„ ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ์ƒˆ๋กœ์šด ์ด๋ฒคํŠธ ์ˆ˜์‹ ์„ ์•Œ๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.

์ด๋Š” ๊ตฌ๋…์„ ๋ฉˆ์ถ”๊ฑฐ๋‚˜ ํ˜น์€ subject๊ฐ€ completed๋‚˜ error ์ด๋ฒคํŠธ๋กœ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์ง€์†๋ฉ๋‹ˆ๋‹ค.

 

์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋งจ ์œ—์ค„์ด PublishSubject๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๊ณ , ๊ทธ ์•„๋ž˜ ๋‘ ์ค„์ด ๊ตฌ๋…์ž๋“ค์ž…๋‹ˆ๋‹ค.

์œ„๋กœ ํ–ฅํ•˜๋Š” ํ™”์‚ดํ‘œ๋“ค์€ ๊ตฌ๋…์„ ๋‚˜ํƒ€๋‚ด๊ณ , ์•„๋ž˜๋กœ ํ–ฅํ•˜๋Š” ํ™”์‚ดํ‘œ๋“ค์€ ์ด๋ฒคํŠธ์˜ ๋ฐฉ์ถœ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ธ๋ฐ์š”,

๋‘๋ฒˆ์งธ ์ค„์—์„œ๋Š” 1๋ฒˆ์ด ์ถ”๊ฐ€๋œ ์ดํ›„์— ๊ตฌ๋…์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๋… ์‹œ์  ์ดํ›„์— ๋ฐฉ์ถœ๋˜๋Š” 2, 3 ์ด๋ฒคํŠธ๋งŒ์„ ๋ฐ›๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งˆ์ง€๋ง‰ ์ค„์—์„œ๋Š” 2๋ฒˆ๊นŒ์ง€ ์ถ”๊ฐ€๋œ ์ดํ›„์— ๊ตฌ๋…ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 3๋ฒˆ ์ด๋ฒคํŠธ๋งŒ์„ ๋ฐ›์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋กœ ํ•œ ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค!

 example(of: "PublishSubject") {
     let subject = PublishSubject<String>()
     subject.onNext("Is anyone listening?")

     // ์ฒซ๋ฒˆ์งธ subscriber
     let subscriptionOne = subject
         .subscribe(onNext: { (string) in
             print(string)
         })
     subject.on(.next("1"))
     subject.onNext("2")

     // 1
     // ๋‘๋ฒˆ์งธ subscriber
     let subscriptionTwo = subject
         .subscribe({ (event) in
             print("2)", event.element ?? event)
         })

     // 2
     subject.onNext("3")
     
     // Print: 3
     // Print: 2) 3

     // 3
     subscriptionOne.dispose()
     subject.onNext("4")
     
     // Print: 2) 4

     // 4
     subject.onCompleted()
     
     // Print: 2) completed

     // 5
     subject.onNext("5")

     // 6
     subscriptionTwo.dispose()

     let disposeBag = DisposeBag()

     // 7
     subject
         .subscribe {
             print("3)", $0.element ?? $0)
     }
         .disposed(by: disposeBag)
         
    // Print: 3) completed

     subject.onNext("?")
 }

์ฒ˜์Œ์— ์‹œ์ž‘ํ•˜๋ฉด์„œ ๋ดค์—ˆ๋˜ ์ฝ”๋“œ ์•„๋ž˜ ๋ถ€ํ„ฐ ์ฃผ์„์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์‚ดํŽด๋ณผ๊ฒŒ์š”!

 

1. ๋‘๋ฒˆ์งธ subscriber ์ถ”๊ฐ€ 

subscriptionTwo๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋‘๋ฒˆ์งธ ๊ตฌ๋…์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒคํŠธ๋“ค์€ ์˜ต์…”๋„ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ด ๊ฐ’๋“ค์€ next ์ด๋ฒคํŠธ๋กœ ๋ฐฉ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— nil ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ nil์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ฐ’์ด ํ”„๋ฆฐํŠธ๋˜๋„๋กํ•˜๊ณ  nil์ด๋ผ๋ฉด ์ด๋ฒคํŠธ๋งŒ ํ”„๋ฆฐํŠธ๋˜๋„๋ก ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

2. ๊ตฌ๋…์ž๊ฐ€ ๋‘ ๋ช…์ธ ์ƒํƒœ์—์„œ subject์— ์ƒˆ๋กœ์šด ๊ฐ’ ์ถ”๊ฐ€

subject์— "3"์ด๋ผ๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ ๊ตฌ๋…์ž๊ฐ€ subscriptionOne, subscriptionTwo ๋‘ ๋ช…์ด๊ธฐ ๋•Œ๋ฌธ์— 3์ด๋ผ๋Š” ๊ฐ’์€ ๊ตฌ๋…์ž๋“ค์— ์˜ํ•ด ํ•œ ๋ฒˆ์”ฉ ์ถœ๋ ฅ๋˜์–ด ์ด ๋‘ ๋ฒˆ ํ”„๋ฆฐํŠธ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

3
2) 3

 

3. subscriptionOne ๊ตฌ๋… ์ค‘๋‹จ ํ›„ subject์— ์ƒˆ๋กœ์šด ๊ฐ’ ์ถ”๊ฐ€

.dispose()๋ฅผ ํ†ตํ•ด ์ฒซ๋ฒˆ์งธ ๊ตฌ๋…์ž์˜ ๊ตฌ๋…์„ ์ค‘๋‹จ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด์ œ ๊ตฌ๋…์ž๊ฐ€ subscriptionTwo ๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ดํ›„ ์ถ”๊ฐ€๋˜๋Š” ๊ฐ’์ธ "4"๋Š” ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

2) 4

 

4. subject ์ข…๋ฃŒ

PublishSubject๊ฐ€ completed๋‚˜ error ๊ฐ™์€ ์ข…๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค๋ฉด, subject๋Š” ๋” ์ด์ƒ ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ์ƒˆ๋กœ์šด next ์ด๋ฒคํŠธ๋“ค์„ ๋ฐฉ์ถœํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, subject๋Š” ์ด๋Ÿฌํ•œ ์ข…๋ฃŒ ์ด๋ฒคํŠธ๋“ค์„ ์ดํ›„ ์ƒˆ subscriber๋“ค์—๊ฒŒ ์žฌ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ completed๊ฐ€ ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

2) completed

 

5~7. subject ์ข…๋ฃŒ๋œ ์ดํ›„ ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž ์ถ”๊ฐ€

subject๋Š” completed ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„ ์ด์ œ ์ข…๋ฃŒ๋˜์—ˆ์œผ๋‹ˆ 5๋ฒˆ์—์„œ ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ถ”๊ฐ€๋˜์ง€๋งŒ ์ด๋ฅผ ๋ฐฉ์ถœํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

6๋ฒˆ์—์„œ๋Š” ๋‘ ๋ฒˆ์งธ ๊ตฌ๋…์ž๊ฐ€ ๊ตฌ๋…์„ ์ค‘๋‹จํ•˜๊ณ  7๋ฒˆ์—์„œ๋Š” ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋˜๋Š”๋ฐ์š”, subject๋Š” ์ด๋ฏธ ์ข…๋ฃŒ๋œ ์ƒํƒœ์—์„œ ์ƒˆ๋กœ์šด subscriber๊ฐ€ ์ƒ๊ธด๋‹ค๊ณ  ํ•ด์„œ ์ข…๋ฃŒ๋œ subject๊ฐ€ ๋‹ค์‹œ ์ž‘๋™ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, .completed ์ด๋ฒคํŠธ๋งŒ์„ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

3) completed

 

์ด๋ฅผ ํ†ตํ•ด subject๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๊ตฌ๋…์ž๋“ค์—๊ฒŒ๋งŒ ์ข…๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ทธ ์ดํ›„์— ๊ตฌ๋…ํ•œ subscriber์—๊ฒŒ๋„ ์ข…๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ ์•Œ๋ ค์ค€๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋•Œ๋•Œ๋กœ ๋ฒ„๊ทธ์˜ ์›์ธ์ด ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

BehaviorSubject

BehaviorSubject๋Š” ๋งˆ์ง€๋ง‰ next ์ด๋ฒคํŠธ๋ฅผ ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž์—๊ฒŒ ๋ฐ˜๋ณตํ•œ๋‹ค๋Š” ์ ๋งŒ ๋นผ๋ฉด PublishSubject์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ํ•œ ๋ฒˆ์— ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ์š”, ์ฒซ ์ค„์ด BehaviorSubject์ด๊ณ  ๊ทธ ์•„๋ž˜ ๋‘ ์ค„์ด subscriber๋“ค์ž…๋‹ˆ๋‹ค!

์ฒซ๋ฒˆ์งธ ๊ตฌ๋…์ž์ธ ๋‘๋ฒˆ์งธ ์ค„์€ 1๋ฒˆ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋œ ์ดํ›„์— ๊ตฌ๋…์„ ์‹œ์ž‘ํ•˜๋Š”๋ฐ, ๊ตฌ๋… ์ฆ‰์‹œ ๋งˆ์ง€๋ง‰ ์ด๋ฒคํŠธ์ธ 1์„ ๋ฐ›๊ณ  ์ดํ›„์— ๋ฐฉ์ถœ๋˜๋Š” ๊ฐ’๋“ค์„ ๋ฐ›๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ๋…์„ ์‹œ์ž‘ํ•  ๋•Œ ๊ตฌ๋… ์ „์˜ ๋งˆ์ง€๋ง‰ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

 

์ด์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ด…์‹œ๋‹ค!

 // 1
 enum MyError: Error {
     case anError
 }

 // 2
 func print<T: CustomStringConvertible>(label: String, event: Event<T>) {
     print(label, event.element ?? event.error ?? event)
 }

 //3
 example(of: "BehaviorSubject") {

     // 4
     let subject = BehaviorSubject(value: "Initial value")
     let disposeBag = DisposeBag()
 }

 

1. ์—๋Ÿฌ ํƒ€์ž… ์ •์˜

๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋ฅผ Error enum์œผ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

2. print ํ•จ์ˆ˜ ์ •์˜

Generic ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด ์ด๋ฒคํŠธ ๋‚ด์— ๊ฐ’์ด ์žˆ์œผ๋ฉด ๊ฐ’์„ ํ”„๋ฆฐํŠธํ•˜๊ณ , ์—๋Ÿฌ๊ฐ€ ์žˆ์œผ๋ฉด ์—๋Ÿฌ๋ฅผ, nil์ด๋ผ๋ฉด ์ด๋ฒคํŠธ๋งŒ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” print ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

3~4. BehaviorSubject ๊ฐ์ฒด ์ƒ์„ฑ

์ƒˆ๋กœ์šด BehaviorSubject ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

BehaviorSubject๋Š” ํ•ญ์ƒ ์ตœ์‹ ์˜ ๊ฐ’์„ ๋ฐฉ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค!

 

4๋ฒˆ์˜ ํ•˜๋‹จ์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ด…์‹œ๋‹ค.

// 6
subject.onNext("X")

// 5
subject
    .subscribe{
        print(label: "1)", event: $0)
    }
    .disposed(by: disposeBag)
        
// 7
subject.onError(MyError.anError)
        
// 8
subject
    .subscribe {
        print(label: "2)", event: $0)
    }
    .disposed(by: disposeBag)

 

 

5. ์ƒ์„ฑํ•œ subject๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  dispose

1) Initial value

์œ„์—์„œ ์ƒ์„ฑํ•œ subject๋ฅผ ๊ตฌ๋…ํ•˜๋ฉด, subject ์ƒ์„ฑ ์ดํ›„ ๋‹ค๋ฅธ ๊ฐ’์ด ์ถ”๊ฐ€๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์ด ์ดˆ๊ธฐ๊ฐ’์ด ํ”„๋ฆฐํŠธ ๋ฉ๋‹ˆ๋‹ค.

 

6. subject๋ฅผ ๊ตฌ๋…ํ•˜๊ธฐ ์ „์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด

1) X

5๋ฒˆ ์ฝ”๋“œ ์ƒ๋‹จ์— subject์— "X"๋ฅผ ๊ฐ€์ง„ .onNext(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด subject๋ฅผ ๊ตฌ๋…ํ•˜๊ธฐ ์ „ ์ตœ์‹ ์˜ ๊ฐ’์ด "X"๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ๊ฐ’์ธ Initial Value๊ฐ€ ์•„๋‹ˆ๋ผ X๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 

 

7~8. subject์— error ์ด๋ฒคํŠธ ์ถ”๊ฐ€ ํ›„ ๊ตฌ๋…ํ•˜๊ธฐ

1) anError
2) anError

subject์— error ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋‘ ๋ช…์˜ ๊ตฌ๋…์ž์— ์˜ํ•ด error ์ด๋ฒคํŠธ๊ฐ€ ํ•œ ๋ฒˆ์”ฉ ์ฐํ˜€ ์ด ๋‘ ๋ฒˆ ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

BehaviorSubject์˜ ๊ฒฝ์šฐ ๋ทฐ๋ฅผ ์ตœ์‹  ๋ฐ์ดํ„ฐ๋กœ ๋ฏธ๋ฆฌ ์ฑ„์šฐ๋ ค๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ํ™”๋ฉด์˜ ์ปจํŠธ๋กค์„ BehaviorSubject์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์•ฑ์ด ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋™์•ˆ ์ตœ์‹  ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ทฐ๋ฅผ ๋ฏธ๋ฆฌ ์ฑ„์›Œ ๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ReplaySubject

ReplaySubject๋Š” ์ƒ์„ฑ ์‹œ ์„ค์ •ํ•œ ํŠน์ • ์‚ฌ์ด์ฆˆ๊นŒ์ง€, ๋ฐฉ์ถœํ•˜๋Š” ์ตœ์‹  ์š”์†Œ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ cacheํ•˜๊ฑฐ๋‚˜ bufferํ•˜๊ณ  ํ•ด๋‹น ๋ฒ„ํผ๋ฅผ ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž์—๊ฒŒ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ฒซ๋ฒˆ์งธ ์ค„์ด ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ๊ฐ€ 2์ธ ReplaySubject, ๊ทธ ์•„๋žซ์ค„๋“ค์€ ๊ตฌ๋…์ž๋“ค์ž…๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ๊ตฌ๋…์ž(์ค‘๊ฐ„์ค„)๋Š” ์ด๋ฏธ subject๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๊ณผ 2๊ฐ€ ๋ฐฉ์ถœ๋˜๋ฉด ์ด๋“ค์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

๋‘๋ฒˆ์งธ ๊ตฌ๋…์ž(๋งˆ์ง€๋ง‰์ค„)๋Š” 2๊ฐ€ ๋ฐฉ์ถœ๋œ ์ดํ›„ ๊ตฌ๋…ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ์ธ 2๋งŒํผ์˜ ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋˜์–ด ๊ตฌ๋… ์ด์ „์— ๋ฐฉ์ถœ๋œ 1๊ณผ 2๋ฅผ ๋ชจ๋‘ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ReplaySubject์˜ ๋ฒ„ํผ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๋‚˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํฌ๊ฒŒ ์ฐจ์ง€ํ•˜๋Š” ๊ฐ’๋“ค์„ ํฐ ์‚ฌ์ด์ฆˆ์˜ ๋ฒ„ํผ๋กœ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ๋ฉ”๋ชจ๋ฆฌ์— ์—„์ฒญ๋‚œ ๋ถ€ํ•˜๋ฅผ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋กœ ์‚ดํŽด๋ด…์‹œ๋‹ค.

 example(of: "ReplaySubject") {

     // 1
     let subject = ReplaySubject<String>.create(bufferSize: 2)
     let disposeBag = DisposeBag()

     // 2
     subject.onNext("1")
     subject.onNext("2")
     subject.onNext("3")

     // 3
     subject
         .subscribe {
             print(label: "1)", event: $0)
         }
         .disposed(by: disposeBag)

     subject
         .subscribe {
             print(label: "2)", event: $0)
         }
         .disposed(by: disposeBag)
 }

 

1. subject ์ƒ์„ฑ

๋ฒ„ํผ ์‚ฌ์ด์ฆˆ๊ฐ€ 2์ธ ReplaySubject๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ReplaySubject๋Š” .create(bufferSize:) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. subject์— ์ƒˆ๋กœ์šด ๊ฐ’ ์ถ”๊ฐ€

์ƒ์„ฑํ•œ subject์— 1, 2, 3์ด๋ผ๋Š” ์„ธ ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

3. subject ๊ตฌ๋…

1) 2
1) 3
2) 2
2) 3

subject์— ๋Œ€ํ•œ ๊ตฌ๋…์ž๋“ค์„ ๋‘ ๊ฐœ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ์œ„์—์„œ ์ถ”๊ฐ€ํ•œ ๊ฐ’๋“ค ์ค‘ ์–ด๋–ค ๊ฐ’์ด ์ถœ๋ ฅ๋ ๊นŒ์š”??

๋งˆ์ง€๋ง‰ 2๊ฐœ์˜ ๊ฐ’๋“ค์ด ๊ฐ๊ฐ์˜ ๊ตฌ๋…์ž๋“ค์— ์˜ํ•ด ํ•œ ๋ฒˆ์”ฉ ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ๊ฐ€ 2์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค!

 

3๋ฒˆ ์ฝ”๋“œ ํ•˜๋‹จ์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 subject.onNext("4")

 subject.subscribe {
     print(label: "3)", event: $0)
     }
     .disposed(by: disposeBag)

์ƒˆ๋กœ์šด ๊ฐ’์ธ "4"๋ฅผ subject์— ์ถ”๊ฐ€ํ•œ ํ›„, ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

"4"๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ถ”๊ฐ€๋˜๋ฉด ๊ธฐ์กด์˜ ๊ตฌ๋…์ž๋“ค( 1), 2) )์€ ์ด ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋  ๊ฒƒ์ด๊ณ , ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž๋Š” ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ์ธ 2๋งŒํผ ์ตœ์‹ ์˜ ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋˜๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

1) 4
2) 4
3) 3
3) 4

 

๊ทธ๋ ‡๋‹ค๋ฉด subject๊ฐ€ error ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ถœ๋ ฅ๋ ๊นŒ์š”?? 

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ subject.onNext("4") ํ•˜๋‹จ(์ƒˆ ๊ตฌ๋…์ž ์ƒ์„ฑ ์ „)์— ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

subject.onError(MyError.anError)

/* Prints:
1) 4
2) 4
1) anError
2) anError
3) 3
3) 4
3) anError
*/

subject๊ฐ€ error๋ฅผ ํ†ตํ•ด ์ข…๋ฃŒ๋˜๊ณ  ๋‚œ ์ดํ›„์˜ ๊ตฌ๋…์ž์ธ 3)์—๊ฒŒ error ์ด๋ฒคํŠธ๋ฅผ ์žฌ์ „์†กํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋•Œ 3)์—๊ฒŒ ๋ฒ„ํผ์— ์žˆ๋Š” ๊ฐ’๋“ค ๋˜ํ•œ ์ „๋‹ฌํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ๋Š” subject๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์–ด๋„ ๋ฒ„ํผ๋Š” ์—ฌ์ „ํžˆ ๋Œ์•„๋‹ค๋‹ˆ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. error๋ฅผ ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ์— ์ด๋ฒคํŠธ์˜ ์žฌ๋ฐฉ์ถœ์„ ๋ง‰์œผ๋ ค๋ฉด dispose()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

subject.dispose()

 /* Prints:
  3) Object `RxSwift.(ReplayMany in _33052C2CE59F358A8740AFDD4371DD39)<Swift.String>` was already disposed.
  */

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด, ์ƒˆ๋กœ์šด ๊ตฌ๋…์ž๋Š” ์—๋Ÿฌ ์ด๋ฒคํŠธ๋งŒ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ตฌ๋… ์ „์— subject ์ž์ฒด๊ฐ€ dispose ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜, ReplaySubject์— ๋ช…์‹œ์ ์œผ๋กœ dispose()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•„์š”ํ•œ ์ž‘์—…์€ ์•„๋‹™๋‹ˆ๋‹ค.

์™œ๋ƒํ•˜๋ฉด ๋งŒ์•ฝ subject์˜ ๊ตฌ๋…์„ disposeBag์— ๋„ฃ๊ณ , ViewController๋‚˜ ViewModel ๊ฐ™์€ ํ•ด๋‹น subject์˜ ์†Œ์œ ์ž๊ฐ€ ํ• ๋‹น ํ•ด์ œ๋˜๋ฉด ๋ชจ๋“  ๊ฒƒ๋“ค์ด dispose ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ Subject์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.. ํž˜๋“œ๋„ค์š”...

Subject๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๊ณ  ์•ž์œผ๋กœ๋„ RxSwift ๊ฒŒ์‹œ๊ธ€์„ ๊พธ์ค€ํžˆ ์˜ฌ๋ ค๋ณด๋„๋ก ๋…ธ๋ ฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค..ใ…Ž