๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

์—ฌํ›ˆ๋‹˜์€ RxSwift๋ฅผ ์ฝ”๋“œ๋ ˆ๋ฒจ์—์„œ ๋ถ„์„ํ•ด๋ณด์‹  ์ ์ด ์žˆ๋‚˜์š”?

์ตœ๊ทผ ๋ฉด์ ‘์—์„œ Rx์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ๋ฐ›์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋•Œ ๋ฌด์Šจ ๋ง์„ ํ–ˆ๋Š”์ง€ ์ž˜ ๊ธฐ์–ต์ด ์•ˆ๋‚˜์ง€๋งŒ.. Rx์— ๋Œ€ํ•ด์„œ ์ดํ•ด๋ฅผ ์ œ๋Œ€๋กœ ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ์—ˆ์Œ์„ ๊นจ๋‹ซ๊ณ  ์ฝ”๋“œ๋ ˆ๋ฒจ๋กœ ํ•œ ๋ฒˆ ๋œฏ์–ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

Observable

๋จผ์ € ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” Observable์˜ ํŒŒ์ผ๋ถ€ํ„ฐ ์ฐพ์•„๊ฐ€๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ผ๋‹จ ํด๋ž˜์Šค์ด๊ณ  ๋‚ด๋ถ€์— subscribe์™€ asObservable ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋„ค์š”. ์ผ๋‹จ asObservable์€ ์ž๊ธฐ ์ž์‹ ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๊ณ , subscribe๋Š” rxAbstractMethod()๋ผ๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ์™ธ์—๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ rxAbstractMethod()๋ฅผ ์•Œ๊ธฐ์œ„ํ•ด ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” Rx.swift์— ์ฐพ์•„๊ฐ€๋ณด์•˜๋Š”๋ฐ์š”,

์ด๋ ‡๊ฒŒ ์ •์˜๊ฐ€ ๋˜์–ด์žˆ๋„ค์š”. ์ด ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ด๊ณ , ์ด ์ฝ”๋“œ๊ฐ€ ๋ถˆ๋ฆฌ๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ subscribe๋Š” Observable ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฐ ์—ฌ๋Ÿฌ Observable์˜ ํด๋ž˜์Šค๋“ค์ด ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๋” ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— Observable์—๋Š” ObservableType์ด ๋ถ™์–ด์žˆ์œผ๋‹ˆ ์ด ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋จผ์ € ์•Œ์•„๋ณผ๊ฒŒ์š”.

ObservableType

ObservableType์€ ํ”„๋กœํ† ์ฝœ์ด๋„ค์š”. ๋‚ด๋ถ€์—๋Š” subscribe์— ๋Œ€ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์˜๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์˜คํ‚ค์˜คํ‚ค subscribe๋Š” Observable์ด ๋“ค๊ณ  ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ObservableType์„ ์ฑ„ํƒํ•˜๊ณ  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด์—ˆ๋„ค์š”!

subscribe์— ๋Œ€ํ•ด ๊ธธ๊ฒŒ ์ ํ˜€์žˆ๋Š” ์ฃผ์„์„ ํ•ด์„ํ•ด๋ณผ๊ฒŒ์š”.

/** Subscribes `observer` to receive events for this sequence.
์˜ต์ €๋ฒ„๊ฐ€ ์ด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ๋…ํ•ฉ๋‹ˆ๋‹ค.

### Grammar **Next\* (Error | Completed)?**
sequences can produce zero or more elements so zero or more `Next` events can be sent to `observer`
์‹œํ€€์Šค๋“ค์€ 0๊ฐœ ์ด์ƒ์˜ ์•„์ดํ…œ์„ Next ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ์˜ต์ €๋ฒ„์— ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

once an `Error` or `Completed` event is sent, the sequence terminates and can't produce any other elements
Error๋‚˜ Completed ์ด๋ฒคํŠธ๊ฐ€ ์ „์†ก๋˜๋ฉด, ์ด ์‹œํ€€์Šค๋Š” ์ข…๋ฃŒ๋˜๊ณ  ๋‹ค๋ฅธ ์•„์ดํ…œ์„ ์ƒ์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

It is possible that events are sent from different threads, but no two events can be sent concurrently to `observer`.
์ด๋ฒคํŠธ๋“ค์ด ์„œ๋กœ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ์ „์†ก๋˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋‘ ์ด๋ฒคํŠธ๊ฐ€ ๋™์‹œ์— ์˜ต์ €๋ฒ„์— ์ „์†ก๋  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.

### Resource Management
When sequence sends `Complete` or `Error` event all internal resources that compute sequence elements will be freed.
์‹œํ€€์Šค๊ฐ€ Complete์ด๋‚˜ Error ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋‚ด๋ฉด, ์‹œํ€€์Šค์—์„œ ์‚ฌ์šฉํ•˜๋˜ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋“ค์€ ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค.

To cancel production of sequence elements and free resources immediately, call `dispose` on returned subscription.
์‹œํ€€์Šค๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์‚ฐํ•˜๋Š” ๊ฒƒ์„ ์ข…๋ฃŒํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณง๋ฐ”๋กœ ํ•ด์ œํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๋ฐ˜ํ™˜๋œ ๊ตฌ๋…๊ฐ์ฒด์— dispose๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

- returns: Subscription for `observer` that can be used to cancel production of sequence elements and free resources. */
Observer๋ฅผ ์œ„ํ•œ ๊ตฌ๋…๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ์‹œํ€€์Šค๋ฅผ ์ทจ์†Œํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

Subscribe์— ๋Œ€ํ•œ ์„ค๋ช…์ด์—ˆ๋Š”๋ฐ์š”, ๊ฒฐ๊ตญ subscribe๋Š” ์ธ์ž๋กœ ๋ฐ›์€ ์˜ต์ €๋ฒ„๋ฅผ ์‹œํ€€์Šค์— ์—ฐ๊ฒฐํ•˜๊ณ  ์‹œํ€€์Šค๋ฅผ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋Š” subscription ๊ฐ์ฒด์ธ Disposable์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ ObservableType์— ๋Œ€ํ•œ Extension๋„ ์žˆ์œผ๋‹ˆ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ค.

asObservable์„๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”, Observableํƒ€์ž…์ด ๊ฐ€์ง„ create ๋ฉ”์„œ๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์„œ ์ „๋‹ฌ๋œ ์ธ์ž๋ฅผ subscribe ๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

create ๋ฉ”์„œ๋“œ์˜ ์ •์˜๋Š” RxSwift/Observables/Create.swift ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ์–ด์š”.

create๋Š” ํƒ€์ž…๋ฉ”์„œ๋“œ ์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์œ„์—์„œ๋„ Observable.create๋กœ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์—ˆ์ฃ . ์ธ์ž๋กœ๋Š” escaping closure๋ฅผ ํ•˜๋‚˜ ๋ฐ›๋Š”๋ฐ์š”, subscribe์˜ ์ •์˜์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์„์„ ๋ณด๋‹ˆ ๊ฐ ์˜ต์ €๋ฒ„๋ธ”์˜ subscribe๋ฉ”์„œ๋“œ ๊ตฌํ˜„์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ผ๊ณ  ํ•˜๋„ค์š”. ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ๋Š” ์‹œํ€€์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ „๋‹ฌ๋œ subscribe ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹œํ€€์Šค๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๋ฌด์Šจ ์†Œ๋ฆฐ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ์‹ค์ œ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ํƒ€์ž…์ธ AnonymousObservable์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ์–ด์š”..

AnonymousObservable

AnonymousObservable๋„ ๊ฐ™์€ Create.swiftํŒŒ์ผ ์•ˆ์— ์ •์˜๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € typealias๊ฐ€ ๋ˆˆ์— ๋„๋Š”๋ฐ์š”, subscribe์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํ˜•์‹์ด ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ตฌ๋…์ด ์žˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ํด๋กœ์ €๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐํ™” ์‹œ์ ์—์„œ ์ด ํด๋กœ์ €๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ธฐ๋„ ํ•˜๋„ค์š”.

์•„๊นŒ create ๋ฉ”์„œ๋“œ์—์„œ AnonymousObservable ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฉด์„œ subscribe ๋ฉ”์„œ๋“œ์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ–ˆ๋Š”๋ฐ์š”, ์ด ๊ตฌํ˜„์ฒด๊ฐ€ subsribeHandler๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์กฐ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์ œ์ผ ์ค‘์š”ํ•ด๋ณด์ด๋Š” run ๋ฉ”์„œ๋“œ๋ฅผ ๋ณผ๊นŒ์š”?

run method๋„ ๋‚ด๋ถ€๋Š” ๊ฝค ๋‹จ์ˆœํ•œ๋ฐ์š”, ์ฒ˜์Œ๋ณด๋Š” ์• ๋“ค์ด ๋งŽ๋„ค์š”..

 

๋จผ์ € ์ธ์ž๋กœ๋Š” ObservableType์„ ์ฑ„ํƒํ•˜๋Š” Observer ํƒ€์ž…๋“ค, Cacelable ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜๊ณ , sink์™€ subscription์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŠœํ”Œ๋กœ ๊ฐ๊ฐ Disposable์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹ด๊ฒจ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

sink์— ์ €์žฅ๋˜๋Š” ๊ฐ’์€ AnonymousObservableSink์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋˜ ์ฐพ์•„๊ฐ€๋ดค์–ด์š”.

์˜ค ์—ฌ๊ธฐ์„  ๊ทธ๋ž˜๋„ ์ต์ˆ™ํ•œ ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ๋ณด์ž…๋‹ˆ๋‹ค. on ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋„ค์š”..! on์ด ํ˜ธ์ถœ๋˜๋ฉด ์ธ์ž๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋“ค์–ด์˜ค๊ณ  ์ด ์ด๋ฒคํŠธ๊ฐ€ next๋ผ๋ฉด forwardOn๋ฉ”์„œ๋“œ์— ์ด๋ฒคํŠธ๋ฅผ ๋‹ด์•„ ๋ณด๋‚ด๊ณ , error๋‚˜ completed๋ผ๋ฉด forwardOn์— ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋‚ธ ํ›„์— ์‹œํ€€์Šค๋ฅผ dispose ์‹œํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  run ๋ฉ”์„œ๋“œ์—์„œ๋Š” Parent ํƒ€์ž…(AnonymousObservable<Element>)์„ ๋ฐ›์•„ ์ž๊ธฐ์ž์‹ ์„ AnyObserver๋กœ ์บ์ŠคํŒ…ํ•œ ๋’ค์— Parent๊ฐ€ ๊ฐ€์ง„ subscribe ๋ฉ”์„œ๋“œ์— ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉฐ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒฐ๊ณผ์ธ Disposable์„ ๋ฐ˜ํ™˜ํ•ด์š”.

forwardOn์€ ์–ด๋””์— ์žˆ์„๊นŒ์š”? AnonymousObservableSink์˜ ์„ ์–ธ๋ถ€๋ฅผ ๋ณด๋‹ˆ Sink<Observer>์™€ OberverType ๋‘ ๊ฐ€์ง€๋ฅผ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์Šˆํผํด๋ž˜์Šค์ธ Sink ํด๋ž˜์Šค๋ฅผ ์ฐพ์•„๊ฐ€ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

forwardOn๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ž์‹ ์ด ๊ฐ€์ง„ observer์˜ on ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜€์Šต๋‹ˆ๋‹ค.

์˜์‹์˜ ํ๋ฆ„ ์ •๋ฆฌํ•˜๊ธฐ - 1

์ž ๊ทธ๋Ÿผ ์ง€๊ธˆ ์•ฝ๊ฐ„ ๋งฅ๋ฝ์„ ์žƒ์€ ๊ฒƒ ๊ฐ™์œผ๋‹ˆ๊นŒ.. (์ €๋„ ์Šคํฌ๋กค์„ ๊ณ„์† ์˜ฌ๋ ธ๋‹ค ๋‚ด๋ ธ๋‹ค ํ•˜๊ณ  ์žˆ์–ด์š”..) ์ง€๊ธˆ๊นŒ์ง€์˜ ์˜์‹์˜ ํ๋ฆ„์„ ์ •๋ฆฌํ•ด๋ณผ๊ฒŒ์š”.

1. ๋จผ์ € ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” Observable์ด ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋Š”์ง€ ์•Œ๊ณ ์‹ถ์—ˆ์–ด์š”. ๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค ๋ดค๊ณ  ๋‚ด๋ถ€์—๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ธ subscribe์™€ asObservable์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์ฒด๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์ฃ . ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋กœ subscribe๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœํ† ์ฝœ์ธ ObservableType์ด ์กด์žฌํ–ˆ์–ด์š”. ๋ชจ๋“  Observable๋“ค์€ ์ด ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๊ตฌํ˜„์ฒด๋ฅผ ๊ฐ๊ฐ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฑฐ์ฃ .

2. asObservable์—์„œ๋Š” Observable.create๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ์š”, ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” create ๋ฉ”์„œ๋“œ๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด์„œ create์˜ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

3. ๊ทผ๋ฐ ๋ง‰์ƒ create๋ฅผ ์—ด์–ด๋ณด๋‹ˆ๊นŒ ๋ณ„๊ฑฐ ์—†์—ˆ์–ด์š”.. ์ „๋‹ฌ๋ฐ›์€ subscribe ๋ฉ”์„œ๋“œ์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ AnonymousObservable ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์—ˆ์ฃ .

4. ๊ทธ๋ž˜์„œ AnonymousObservable์„ ์—ด์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ subscribe ๋ฉ”์„œ๋“œ๋ฅผ subscribeHandler๋ผ๋Š” ์ด๋ฆ„์˜ typealias๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

5. ๊ทธ๋ฆฌ๊ณ  ๋‚ด๋ถ€์— ๋”ฑ ํ•˜๋‚˜ ์žˆ๋˜ ๋ฉ”์„œ๋“œ๋Š” run ๋ฉ”์„œ๋“œ์˜€์–ด์š”. ์ด ๋ฉ”์„œ๋“œ๋Š” sink ์™€ subscription์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋‘ Disposable์„ ํŠœํ”Œ๋กœ ๋ฌถ์–ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์˜€์Šต๋‹ˆ๋‹ค.

6. ์ด์ œ๋Š” sink๋ž‘ subscription์ด ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์ง€๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ์ฃ . sink๋ฅผ ๋ดค๋”๋‹ˆ AnonymousObservableSink์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  subscription์€ ์ด AnonymousObservableSink์˜ ๋ฉ”์„œ๋“œ ์ค‘ run์„ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์—ˆ์–ด์š”. run์„ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์ธ์ž๋กœ ๋ฐ›์€ ์˜ต์ €๋ฒ„๊ฐ€ ์•„๋‹ˆ๋ผ ํ˜„์žฌ ์ธ์Šคํ„ด์Šค ์ž๊ธฐ ์ž์‹ ์„ ์ „๋‹ฌํ•˜๊ณ  ์žˆ์—ˆ์ฃ .

7. AnonymousObservableSink ์•ˆ์—๋Š” ์–ด๋”˜๊ฐ€ ์ต์ˆ™ํ•œ on ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ์–ด์š”. ์ด ๋ฉ”์„œ๋“œ๋Š” ์ „๋‹ฌ๋œ ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ์„œ next๊ฐ€ ์ „๋‹ฌ๋˜๋ฉด forwardOn์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์—ˆ๊ณ , error๋‚˜ complete ์ด๋ฒคํŠธ์—๋Š” forwardOn ์ดํ›„์— dispose๊นŒ์ง€ ํ˜ธ์ถœํ•ด์„œ ์‹œํ€€์Šค๋ฅผ ๋‹ซ์•„์ฃผ๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

8. ๊ทธ๋Ÿผ forwadOn๋„ ํ™•์ธํ•ด๋ด์•ผ๊ฒ ์ฃ ? fowardOn์€ ๋ถ€๋ชจํด๋ž˜์Šค์ธ Sink<Observer>์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ์˜€๊ณ , ์ž์‹ ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์ธ observer์˜ on์„ ํ˜ธ์ถœํ•ด์ฃผ๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์˜ค ๋ฌด์Šจ ์†Œ๋ฆฐ์ง€ ๋ชจ๋ฅด๊ฒ ์ฃ ? ์ €๋„ ๊ทธ๋ž˜์š”..

run ๋‹ค์‹œ๋ณด๊ธฐ

์–ด๋””์—์„œ ํ๋ฆ„์„ ๋†“์ณค๋Š”์ง€ ๊ณฐ๊ณฐํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ค๊ฐ€

AnonymousObservableSink์—์„œ run ๋ฉ”์„œ๋“œ ๋ถ€๋ถ„์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”. parent ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ ๊นŒ์ง€๋Š” ๊ทธ๋ ‡๋‹ค ์น˜๋Š”๋ฐ.. subscribeHandler๋Š” ๋ถ„๋ช… ํด๋กœ์ €๋ฅผ ์ €์žฅํ•˜๋Š”๋ฐ ์ €๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ค์ง€..? ๋ผ๋Š” ์ƒ๊ฐ์ด์š”.

๊ทธ๋ž˜์„œ AnyObserver๋ฅผ ๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‚ด๋ถ€์—๋Š” ๋‘ ์ข…๋ฅ˜์˜ ์ƒ์„ฑ์ž๊ฐ€ ์กด์žฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”, ํ•˜๋‚˜๋Š” EventHandler ํด๋กœ์ €๋ฅผ ๋ฐ›๋Š” ์ƒ์„ฑ์ž์ด๊ณ , ํ•˜๋‚˜๋Š” ObserverType ์„ ๋ฐ›๋Š” ํด๋กœ์ €๊ฐ€ ์žˆ์–ด์š”.

์•„๊นŒ run ๋ฉ”์„œ๋“œ์—์„œ๋Š” self๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์žˆ์—ˆ์œผ๋‹ˆ๊นŒ ObserverType์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š” AnonymousObservableSink ์ธ์Šคํ„ด์Šค๊ฐ€ ์ „๋‹ฌ์ด ๋˜์—ˆ๊ฒ ์ฃ . ๊ทธ๋Ÿผ ๋‘๋ฒˆ์งธ ์ƒ์„ฑ์ž์—์„œ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ด์•ผ๊ฒ ๋„ค์š”.

์˜ต์ €๋ฒ„์˜ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š” ํด๋กœ์ €๋ฅผ ์ž์‹ ์˜ EventHandler๋กœ ํ• ๋‹นํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์‹œ ์ด ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€๋ฉด

run ๋ฉ”์„œ๋“œ๋Š” ์—ฌ๊ธฐ์„œ subscription์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœ์ด ๋˜๋Š”๋ฐ์š”, run ํ˜ธ์ถœ๊ณผ ํ•จ๊ป˜ ์ „๋‹ฌ๋œ Observer๋กœ sink๋ผ๋Š” ๋ณ€์ˆ˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ž๊ธฐ์ž์‹ ์„ sink.run์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•ด subscriptionHandler๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ๊ฒฐ๊ตญ์—๋Š” ์ƒ์„ฑ๋œ AnonymousObservable์—์„œ run์„ ํ˜ธ์ถœํ•˜๋ฉด ํ˜ธ์ถœ๊ณผ ํ•จ๊ป˜ ์ „๋‹ฌ๋œ ์˜ต์ €๋ฒ„์— ๋Œ€ํ•œ AnonymousObservableSink ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ run์„ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ž๊ธฐ์ž์‹ ์„ ์ „๋‹ฌํ•˜๋ฉด ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋˜ subscribe ๋ฉ”์„œ๋“œ์˜ ๊ตฌํ˜„์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ž์˜ ์˜ต์ €๋ฒ„๋กœ AnonymousObservableSink์ธ์Šคํ„ด์Šค๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์˜ค ๋ญ”๊ฐ€ ์ข€ ๋Š๋‚Œ์ด ์˜ค์ง€ ์•Š๋‚˜์š”??

์ฝ”๋“œ๋ž‘ ๊ฐ™์ด ๋ณด๊ธฐ

Observable<String>.create({ (observer) -> Disposable in 
	observer.onNext("1") 
    return Disposables.create() 
})

create๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ์ฃ ?

๊ทธ๋Ÿผ ์•„๊นŒ ๋ดค์—ˆ๋˜ ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋  ํ…๋ฐ์š”, Eelemt๋Š” ๋Ÿฐํƒ€์ž„์— String ์œผ๋กœ ์ •ํ•ด์งˆํ…Œ๊ณ , ๋‚ด๋ถ€ ๊ตฌํ˜„๋Œ€๋กœ AnonymousObservable์ด ์‹คํ–‰๋˜๊ฒ ๋„ค์š”.

๊ทธ๋ฆฌ๊ณ  ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ํด๋กœ์ €๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์—ˆ๋˜

{ (observer) -> Disposable in 
	observer.onNext("1") 
	return Disposables.create() 
}

์ด๊ฒŒ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค! ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ์ง€๋งŒ observer๋ผ๊ณ  ๋งŒ๋“ค์—ˆ๋˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ AnyObserver๋ฅผ ์˜๋ฏธํ•˜๊ณ  ์žˆ์—ˆ๋„ค์š”. ๊ทธ๋ž˜์„œ ๋‚ด๋ถ€์—์„œ onNext, onError ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋˜๊ฑฐ์˜ˆ์š”!

์ž ์ด์ œ ์ด ํด๋กœ์ €์™€ ํ•จ๊ป˜ AnonymousObservable์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.

์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” subscribeHandler๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ๊ทธ ํด๋กœ์ € ๊ทธ๋Œ€๋กœ๊ฒ ์ฃ !

์ž ๊ทธ๋Ÿผ ๋Œ€๋ง์˜ ์ € run ๋ฉ”์„œ๋“œ๊ฐ€ ์–ธ์ œ ํ˜ธ์ถœ๋˜๋Š”์ง€ ์•Œ์•„์•ผ๋˜๋Š”๋ฐ, ์•Œ๊ณ ๋ณด๋‹ˆ๊นŒ AnonymousObservable์ด ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋”๋ผ๊ตฌ์š”.. ๋ฐ”๋กœ Producer ์ž…๋‹ˆ๋‹ค!

์ž ๊น.. Producer๋ถ€ํ„ฐ ๋ณด์ž!

Producer๋Š” Observable์„ ์ƒ์†ํ•˜๊ณ  ์žˆ๊ณ , RxSwift์˜ ๋ชจ๋“  Operator๋“ค์€ ์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ์–ด์š”! ์œ„์— ์žˆ์—ˆ๋˜ run ๋ฉ”์„œ๋“œ๋„ ์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ ๋ฉ”์„œ๋“œ์˜€์Šต๋‹ˆ๋‹ค.

์ด์ œ run์ด ์–ธ์ œ ๋ถˆ๋ฆฌ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ๋˜์—ˆ๋Š”๋ฐ์š”, ๊ตฌํ˜„๋œ subscribe์˜ ๋‚ด๋ถ€๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

subscribe๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์–ด๋–ค ์Šค์ผ€์ค„๋Ÿฌ์ด๋“ ์ง€ ์ƒ๊ด€์—†์ด ํ•˜๋Š” ์ผ๋“ค์ด ์žˆ๋Š”๋ฐ์š”, SinkDisposer ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  run ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ sinkAndSubscription์„ ๋งŒ๋“  ๋’ค์— disposer์— sink์™€ subscription์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

์•„๊นŒ run ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์ด sink์™€ subscription์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋‘ Disposable์ด์—ˆ์ฃ ! ์—ฌ๊ธฐ์„œ ํผ์ฆ์ด ๋งž์ถฐ์ง€๋„ค์š”..!

๊ทธ๋Ÿผ SinkDisposer๊ฐ€ ๋ญ”์ง€ ๋˜ ๋ด์•ผ๊ฒ ์ฃ ?

๋‹ค๋ฅธ๊ฑด ๋ณต์žกํ•˜๋‹ˆ๊นŒ ์•ˆ๋ณด๊ณ  setSinkAndSubscription์—์„œ ๊ฒฐ๊ตญ ํ•˜๋Š” ์ผ์€ ํ˜„์žฌ ์ธ์Šคํ„ด์Šค์˜ sink์™€ subscription์— Disposable๋“ค์„ ๋„ฃ์–ด์ฃผ๋Š”๊ฑฐ์˜ˆ์š”.

๋ญํ•˜๋Ÿฌ ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜ ํ–ˆ๋”๋‹ˆ SinkDisposer์˜ ํ”„๋กœํ† ์ฝœ์ด Cancelable์ด๋„ค์š”..

Cancelable์€ isDisposed ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณตํ•ด์„œ ๊ตฌ๋… ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ”„๋กœํ† ์ฝœ์ด๊ณ , ์ด ์ƒ์œ„์— ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ์ด ์šฐ๋ฆฌ๊ฐ€ ์ตœ์ข…์ ์œผ๋กœ ์ „๋‹ฌ๋ฐ›๋Š” Disposable์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ dispose ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ฃ .

์ž ์ด์ œ ์–ด๋Š์ •๋„ ์ •๋ฆฌ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์‹œ ์ฝ”๋“œ๋กœ

Observable<String>.create({ (observer) -> Disposable in 
	observer.onNext("1") return Disposables.create() 
})

๋‹ค์‹œ ์ด create ๋กœ ์ƒˆ๋กœ์šด ์˜ต์ €๋ฒ„๋ธ”์€ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋กœ ์™€๋ณผ๊ฒŒ์š”. create๋ฅผ ํ•˜๋ฉด์„œ ์ „๋‹ฌํ•œ ํด๋กœ์ €๊ฐ€ AnonymousObservable์— ์ „๋‹ฌ์ด ๋œ๋‹ค๊ณ  ํ–ˆ์ฃ ? ๊ทธ๋Ÿผ AnonymousObservable์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

let observable = Observable<String>.create({ (observer) -> Disposable in 
	observer.onNext("1") return Disposables.create() 
}) 

observable.subscribe( onNext: { data in print(data) })

๊ทธ๋ฆฌ๊ณ  ์ด AnonymousObservable์„ subscribeํ•˜๋ฉด, Producer์— ์žˆ๋Š” subscribe ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜์ฃ .

์•— ๊ทผ๋ฐ subscribe์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์€ Observer์ธ๋ฐ, ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฐ๊ฑฐ ์ค€ ์ ์ด ์—†๋Š”๋ฐ์š”..?

ObservableType+Extension ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ์ด๋Ÿฐ๊ฒŒ ์žˆ๊ฑฐ๋“ ์š”! ์šฐ๋ฆฌ๋Š” ๊ฐ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ํ•ธ๋“ค๋ง ํด๋กœ์ €๋งŒ ์ „๋‹ฌํ•ด์ฃผ์–ด๋„ ๋‚ด๋ถ€์ ์œผ๋กœ AnonymousObserver๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์šฐ๋ฆฌ๋Š” Observer๋ฅผ ๊ฐ„์ ‘์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์žˆ์—ˆ๋˜๊ฑฐ์ฃ ..!

๊ทธ๋Ÿผ run๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ์ด AnonymousObserver๊ฐ€ ์ „๋‹ฌ์ด ๋˜๊ฒ ์ฃ ?

์ด ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค! cancel์—๋Š” ์•„๊นŒ ๋งŒ๋“ค์—ˆ๋˜ disposer๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค!

์ด์ œ AnonymousObserverSink ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ , ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐ„ observer๊ฐ€ on์ด ๋ถˆ๋ ธ์„ ๋•Œ forwardOn์ด ๋ถˆ๋ฆฌ๋ฉด์„œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํด๋กœ์ €์˜ onNext๋ฅผ ์‹คํ–‰ํ•ด์ค„๊ฑฐ์˜ˆ์š”.

subscription์€ sink.run(self)๋กœ ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ์š”, ์—ฌ๊ธฐ์„œ self๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ AnonymousObserver์ด๊ณ  self๋Š” ์ง€๊ธˆ ๊ตฌ๋…ํ•˜๋ ค๋Š” Observable์ด์—์š”. observable.subscribe๋กœ ๋ถˆ๋ €์œผ๋‹ˆ๊นŒ์š”.

๊ทธ๋Ÿผ ์ด ์นœ๊ตฌ๊ฐ€ ์‹คํ–‰์ด ๋  ํ…๋ฐ์š”, ์—ฌ๊ธฐ์„œ parent๋Š” ๋ฐœํ–‰์ž๊ฐ€๋  Observable์ด๊ฒ ์ฃ ? ์–˜๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” subscribeHandler, ์ฆ‰ create์•ˆ์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ๋ฐฉ์ถœ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š” ์ฃผ์ฒด๊ฐ€ AnonymousOberver๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜๋ฉด run์ด ํ˜ธ์ถœ๋˜๋ฉด create์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ์ด๋ฒคํŠธ๋“ค์ด ์‹คํ–‰๋˜๋ฉด์„œ subscribe์—์„œ ๋งŒ๋“  ํด๋กœ์ €๋“ค์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด์ฃ .

์˜์‹์˜ ํ๋ฆ„ ์ •๋ฆฌ - 2

์™€ ์ ˆ๋Œ€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•  ์ค„ ์•Œ์•˜๋Š”๋ฐ ์กฐ๊ธˆ์€ ์ดํ•ด๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.. ์ •๋ฆฌํ•ด๋ณผ๊ฒŒ์š”

1. create๋กœ ์˜ต์ €๋ฒ„๋ธ”์„ ๋งŒ๋“ค๋ฉด create ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ AnonymousObservable์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

Observable<String>.create({ (observer) -> Disposable in 
	observer.onNext("1") 
    return Disposables.create() 
})


2. ์ด ์ฝ”๋“œ์—์„œ create ์•ˆ์— ๋“ค์–ด๊ฐ„ ํด๋กœ์ €๊ฐ€ AnonymousObservable์˜ subscribeHandler๋กœ ์ง€์ •๋œ๋‹ค.

3. subscribe ๋ฉ”์„œ๋“œ๋ฅผ ์–ด๋–ค ์˜ต์ €๋ฒ„๋ธ”์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•˜๋ฉด์„œ ํด๋กœ์ €๋กœ onNext์— ๋Œ€ํ•œ ํด๋กœ์ €๋ฅผ ์ง€์ •ํ•˜๋ฉด ObservableType์—์„œ ์ด ํด๋กœ์ €๋“ค์„ ํฌํ•จํ•˜๋Š” AnonymousObserver๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐœํ–‰์ž์˜ ์ƒ์œ„ ํด๋ž˜์Šค์ธ Producer๊ฐ€ ๊ฐ€์ง„ run ๋ฉ”์„œ๋“œ์— ์ธ์ž๋กœ ๋„˜๊ฒจ ํ˜ธ์ถœํ•œ๋‹ค.

observable.subscribe( onNext: { data in print(data) }) 

// onNext: { data in print(data) } --> AnonymousObserver๋กœ ์ƒ์„ฑ ์ด๋ฆ„์„ AO๋กœ ํ•˜์ž. 

// observable.subscribe(AO) ๊ฐ€ ๋˜๊ณ ,
// Producer์—์„œ๋Š” observable.run(AO, disposer)์„ ํ˜ธ์ถœ

 

4. Producer์—์„œ ํ˜ธ์ถœ๋œ run์—์„œ๋Š” sink์™€ subscription์„ ๋งŒ๋“ ๋‹ค. sink๋Š” ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ์ •์˜ํ•œ ํด๋กœ์ €๋ฅผ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ๋“ฑ๋ก๋œ disposable์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. subsription์€ AnonymousObservableSink ์˜ run ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

5. run ๋ฉ”์„œ๋“œ๋Š” create์—์„œ ๋งŒ๋“ค์–ด์„œ ๋“ฑ๋ก๋œ subscribeHandler๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ์ธ์ž๋กœ subscribe ๋ฉ”์„œ๋“œ์—์„œ ๋งŒ๋“  AnonymousObserver์„ ์ „๋‹ฌํ•œ๋‹ค.

Observable<String>.create({ (observer) -> Disposable in // ์—ฌ๊ธฐ observer๊ฐ€ subscribe์—์„œ ๋งŒ๋“ค์–ด์ง„ AnonymousObserver 
	observer.onNext("1") 
    return Disposables.create() 
})

 

6. AnonymousObserver๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „๋‹ฌ๋˜๊ณ  ๋‚ด๋ถ€์˜ on ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜๋ฆฌ๋œ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

๊ดœํžˆ ์‹œ์ž‘ํ–ˆ๋‹ค..

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

ํ˜น์‹œ ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์‹œ๊ณ  RxSwift์˜ ๋‚ด๋ถ€ ๋™์ž‘์— ๋Œ€ํ•ด ์ž˜ ์ •๋ฆฌ๊ฐ€ ๋œ ์ž๋ฃŒ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ถ”์ฒœํ•ด์ฃผ์„ธ์š”!

๊ธธ๊ณ  ํ˜ผ๋ž€์Šค๋Ÿฌ์šด ๊ธ€ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~!