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

Observable.create

Observable์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ง„์งœ ๋งŽ์ด ์“ฐ๋Š” create ๋ฉ”์„œ๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ create์— ํด๋กœ์ €๋ฅผ ์ „๋‹ฌํ•˜๊ณ  Observable์„ ๋ฐ˜ํ™˜๋ฐ›์Šต๋‹ˆ๋‹ค. 

let observable = Observable<Int>.create { observer in
    observer.onNext(1)
    observer.onCompleted()
    return Disposables.create()
}

observable.subscribe(...)

์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ์—ˆ์ฃ . ๊ทธ๋Ÿผ ์–ด๋–ค ๊ณผ์ •์œผ๋กœ Observable์ด ์ƒ์„ฑ๋˜๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค!

 

๊ฐœ์ธ์ ์œผ๋กœ RxSwift๋ฅผ ๊ฐ„๋žตํ•˜๊ฒŒ ์ฝ”๋“œ๋กœ ์˜ฎ๊ธด ์ €์žฅ์†Œ๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ์š”, ๋‚ด๋ถ€์ ์œผ๋กœ ์›์ž์ ์—ฐ์‚ฐ์ด๋‚˜ ํ™•์žฅ์„ฑ์— ๋Œ€ํ•œ ์ง€์›์ด ๋ถ€์กฑํ•˜์ง€๋งŒ ํ•ต์‹ฌ์ ์ธ ๋กœ์ง์„ ์ดํ•ดํ•˜๊ณ ์ž ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” ์ด ๋ ˆํฌ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ํ™•์ธํ•ด๋ณด๋ ค๊ณ  ํ•ด์š”.

 

GitHub - jeonyeohun/Understanding-RxSwift-Internals: ์ฝ๋‹ค๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋˜๋Š” ์•Œ์—‘์Šค ๐Ÿ‘€

์ฝ๋‹ค๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋˜๋Š” ์•Œ์—‘์Šค ๐Ÿ‘€. Contribute to jeonyeohun/Understanding-RxSwift-Internals development by creating an account on GitHub.

github.com

์‹ค์ œ ์ €์žฅ์†Œ์ธ RxSwift ์˜ ์ฝ”๋“œ์™€ ํ•ต์‹ฌ ๋กœ์ง์€ ์™„์ „ํžˆ ๋˜‘๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ตํ•˜๋ฉด์„œ ๋ด๋„ ๊ดœ์ฐฎ์„๊ฑฐ์—์š”! ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€์ž

let observable = Observable<Int>.create { observer in
    observer.onNext(1)
    observer.onCompleted()
    return Disposables.create()
}

observable.subscribe(...)

์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ˆœ์„œ๋Œ€๋กœ ์–ด๋–ค ์ผ๋“ค์ด ์ผ์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € create ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

create๋Š” Observable์ด ์•„๋‹ˆ๋ผ ObservableType์˜ ํƒ€์ž…๋ฉ”์„œ๋“œ๋กœ ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

Observable์„ ์—ด์–ด๋ณด๋ฉด ObservableType ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ์–ด์š”. ์šฐ๋ฆฌ๊ฐ€ create๋กœ ๋ถˆ๋ €๋˜ ๋ฉ”์„œ๋“œ๋Š” Observable์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ObservableType์˜ ํƒ€์ž…๋ฉ”์„œ๋“œ์˜€๋˜ ๊ฒƒ์ด์ฃ . 

 

๋‹ค์‹œ ์ด ์ฝ”๋“œ๋ฅผ ๋ณผ๊ฒŒ์š”! create๋Š” subscribe๋ผ๋Š” ์ด๋ฆ„์˜ escaping closure๋ฅผ ๋ฐ›๊ณ  ์žˆ์–ด์š”. ์ด ํด๋กœ์ €๋Š” AnyObserver๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  Disposable์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํด๋กœ์ €์ž…๋‹ˆ๋‹ค. 

 

create๋ฅผ ํ•  ๋•Œ ์ „๋‹ฌํ–ˆ๋˜ ํด๋กœ์ €๋ฅผ ๋‹ค์‹œ ๋ณผ๊นŒ์š”?

{ observer in
    observer.onNext(1)
    observer.onCompleted()
    return Disposables.create()
}

์ด ํด๋กœ์ €์ธ๋ฐ, observer๋ผ๋Š” ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ observer์˜ onNext ๋ฉ”์„œ๋“œ์™€ onCompleted ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๋งˆ์ง€๋ง‰์—๋Š” Disposables.create() ๋ฅผ ํ†ตํ•ด์„œ Disposable์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํด๋กœ์ €์ž…๋‹ˆ๋‹ค.

 

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

 

AnonymousObservable์ด ์–ด๋–ค ํด๋ž˜์Šค์ธ์ง€ ํ™•์ธ์„ ํ•ด๋ด์•ผ๊ฒ ๋„ค์š”. AnonymousObservable๋ฅผ ์—ด์–ด๋ด…์‹œ๋‹ค!

 

AnonymousObservable์•ˆ์—๋Š” ์ƒ์„ฑ์ž๋„ ์žˆ๊ณ , run์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•„์ง run์„ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋งŒ๋‚˜์ง€ ๋ชปํ–ˆ์œผ๋‹ˆ๊นŒ ํ๋ฆ„ ๊ทธ๋Œ€๋กœ ์ƒ์„ฑ์ž๋งŒ ํ™•์ธํ•ด๋ณผ๊ฒŒ์š”.

 

์ƒ์„ฑ์ž๋Š” SubscribeHandler ํƒ€์ž…์„ ์ธ์ž๋กœ ๋ฐ›์•„์„œ subscribeHandler ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•ด์ฃผ๊ณ  ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  SubscribeHandler๋Š” ์•„๊นŒ ๋ณด์•˜๋˜ create์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…๊ณผ ๋™์ผํ•œ (AnyObserver) -> Disposable ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ๊ฒฐ๊ตญ ์šฐ๋ฆฌ๊ฐ€ ์ „๋‹ฌํ•œ ํด๋กœ์ €๊ฐ€ AnonymousObservable์˜ subscribeHandler์— ์ €์žฅ๋˜๊ณ , ์ด AnonymousObservable์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ create ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ •๋ฆฌ

create๋Š” ๋๋‚ฌ์Šต๋‹ˆ๋‹คใ…Žใ…Ž create๋Š” ์‚ฌ์‹ค ๋‹จ์ˆœํ•˜๊ณ , subscribe๋ฅผ ๋ด์•ผ ์ด๋ ‡๊ฒŒ ๋งŒ๋“  AnonymousObservable์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ์ผ๋‹จ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ด…์‹œ๋‹ค!

 

1) ๋จผ์ € create ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ํด๋กœ์ €๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

2) create ๋ฉ”์„œ๋“œ๋Š” ObservableType์— ์ •์˜๋˜์–ด ์žˆ๊ณ , ๊ตฌํ˜„์€ AnonymousObservable ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ž‘์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

3) AnonymousObservable์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” create๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ ์ „๋‹ฌํ•œ ํด๋กœ์ €๊ฐ€ subscribeHandler์— ์ €์žฅ์ด ๋ฉ๋‹ˆ๋‹ค. 

 

๊ฐ„๋‹จํ•˜์ฃ ?

 

๋‹ค์Œ ํฌ์ŠคํŠธ์—์„œ๋Š” subscribe์˜ ๋™์ž‘์›๋ฆฌ๋ฅผ ๋ณด๋ ค๊ณ  ํ•ด์š”. ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!