ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

img


๐ŸŽฏ RxSwift ์ดํ•ดํ•˜๊ธฐ_02


RxSwift ์ดํ•ดํ•˜๊ธฐ_01 - https://jayb-log.tistory.com/275

RxSwift ์ดํ•ดํ•˜๊ธฐ_03 - https://jayb-log.tistory.com/277

โ€‹

  • ๐Ÿ™‹ ์ด ๊ธ€์€ RxSwift์— ๋Œ€ํ•œ ์—„์ฒญ๋‚œ ์ดํ•ด๋‚˜ ๋ณธ์งˆ์„ ํŒŒ์•…ํ•œ๋‹ค๊ธฐ๋ณด๋‹ค ์ž…๋ฌธํ•˜๋Š” ์ž…์žฅ์—์„œ ์ž‘์„ฑํ•˜๋Š” ๊ธ€์ด์—์š”! ๊ณฐํŠ€๊น€๋‹˜์˜ ๊ฐ•์˜(ํ•˜๋‹จ์— ๋งํฌ) ๋ฅผ ๋“ค์œผ๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ, ์ƒ๊ฐ๋“ค์„ ์ž‘์„ฑํ•  ์˜ˆ์ •์ด์—์š”. ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด๋‚˜ ์ถ”๊ฐ€ํ•˜๋ฉด ์ข‹์€ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ํ”ผ๋“œ๋ฐฑ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :]

๐Ÿง‘๐Ÿปโ€๐Ÿ’ป Operators๋ž€?


  • ReactiveX์—์„œ๋Š” Operators๋ฅผ ๋ง ๊ทธ๋Œ€๋กœ "์—ฐ์‚ฐ์ž"๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์–ด์š”. ์—ฐ์‚ฐ์ž ์†Œ๊ฐœ๋ฅผ ์ž ์‹œ ๋ณผ๊ฒŒ์š”!


    ReactiveX๋ฅผ ์ง€์›ํ•˜๋Š” ์–ธ์–ด ๋ณ„ ๊ตฌํ˜„์ฒด๋“ค์€ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ž๋“ค์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด ์ค‘์—๋Š” ๊ณตํ†ต์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ์—ฐ์‚ฐ์ž๋„ ์žˆ์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ ํŠน์ • ๊ตฌํ˜„์ฒด์—์„œ๋งŒ ์ œ๊ณตํ•˜๋Š” ์—ฐ์‚ฐ์ž๋“ค๋„ ์กด์žฌํ•œ๋‹ค. ๋˜ํ•œ, ์–ธ์–ด๋ณ„ ๊ตฌํ˜„์ฒด๋“ค์€ ์ด๋ฏธ ์–ธ์–ด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„๊ณผ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋กœ ์—ฐ์‚ฐ์ž์˜ ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋‹ค.


    ReactiveX์—์„œ ์ง€์›ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์–ธ์–ด๊ฐ€ ์žˆ๊ณ , ๊ณตํ†ต์ ์ธ ๋ฉ”์†Œ๋“œ๋„ ์žˆ์ง€๋งŒ ํŠน์ •ํ•œ ์–ธ์–ด์—์„œ๋งŒ ์ œ๊ณต๋˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ์„ค๋ช…์ด์—์š”. RxKotlin์—๋Š” ์—†์ง€๋งŒ RxSwift์—๋Š” ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ด๊ฒ ์ฃ ?


  • Operators Chain

    Optional Chaining์ฒ˜๋Ÿผ ์—ฐ๋‹ฌ์•„ ์ฒ˜๋ฆฌํ•˜๋Š” Chaining ์ž‘์—…์„ Rx์˜ Operators์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ด์—์š”.


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

    ๋”ฐ๋ผ์„œ, Observable์˜ ์—ฐ์‚ฐ์ž ์ฒด์ธ์€ ์›๋ณธ Observable๊ณผ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์—†๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ์ด๋ฏธ ์ดํ•ดํ•˜๊ณ  ์žˆ๋“ฏ์ด ์—ฐ์‚ฐ์ž ์ฒด์ธ์—์„œ ๋จผ์ € ์‹คํ–‰๋œ ์—ฐ์‚ฐ์ž๊ฐ€ ๋ฆฌํ„ดํ•œ Observable์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ ์—ฐ์‚ฐ์ž๊ฐ€ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


  • ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ์—ฐ์‚ฐ์ž

    ์ž์„ธํ•œ ์„ค๋ช…์€ Operators ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”. ์นดํ…Œ๊ณ ๋ฆฌ์—๋Š” ์ƒ์„ฑ, ๋ณ€ํ™˜, ํ•„ํ„ฐ๋ง ๋“ฑ์ด ์žˆ๊ณ  ์˜ˆ๋ฅผ ๋“ค์–ด ์ƒ์„ฑ ์นดํ…Œ๊ณ ๋ฆฌ์—๋Š”


    • Create โ€” ์ง์ ‘์ ์ธ ์ฝ”๋“œ ๊ตฌํ˜„์„ ํ†ตํ•ด ์˜ต์ €๋ฒ„ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Defer โ€” ์˜ต์ €๋ฒ„๊ฐ€ ๊ตฌ๋…ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” Observable ์ƒ์„ฑ์„ ์ง€์—ฐํ•˜๊ณ  ๊ตฌ๋…์ด ์‹œ์ž‘๋˜๋ฉด ์˜ต์ €๋ฒ„ ๋ณ„๋กœ ์ƒˆ๋กœ์šด Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Empty/Never/Throw โ€” ์•„์ฃผ ์ •ํ™•ํ•˜๊ณ  ์ œํ•œ๋œ ํ–‰๋™์„ ํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • From โ€” ๋‹ค๋ฅธ ๊ฐ์ฒด๋‚˜ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ Observable๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค
    • Interval โ€” ํŠน์ • ์‹œ๊ฐ„๋ณ„๋กœ ์—ฐ์†๋œ ์ •์ˆ˜ํ˜•์„ ๋ฐฐ์ถœํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Just โ€” ๊ฐ์ฒด ํ•˜๋‚˜ ๋˜๋Š” ๊ฐ์ฑ„์ง‘ํ•ฉ์„ Observable๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ณ€ํ™˜๋œ Observable์€ ์›๋ณธ ๊ฐ์ฒด๋“ค์„ ๋ฐœํ–‰ํ•œ๋‹ค
    • Range โ€” ์—ฐ์†๋œ ๋ฒ”์œ„(Range)์˜ ์ •์ˆ˜๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Repeat โ€” ํŠน์ • ํ•ญ๋ชฉ์ด๋‚˜ ์—ฐ์†๋œ ํ•ญ๋ชฉ๋“ค์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฐฐ์ถœํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Start โ€” ํ•จ์ˆ˜์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ถœํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค
    • Timer โ€” ์ง€์ •๋œ ์‹œ๊ฐ„์ด ์ง€๋‚˜๊ณ  ๋‚œ ํ›„ ํ•ญ๋ชฉ์„ ํ•˜๋‚˜ ๋ฐฐ์ถœํ•˜๋Š” Observable์„ ์ƒ์„ฑํ•œ๋‹ค

    ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ์–ด์š”.

    ์ด Operators ์ค‘ ์ผ๋ถ€๋ฅผ ํ•˜๋‚˜ ํ•˜๋‚˜ ์‚ดํŽด๋ณด๋„๋ก ํ• ๊ฒŒ์š”!



โš™๏ธ Operators์˜ ์ข…๋ฅ˜


  • Just

    just ์—ฐ์‚ฐ์ž์˜ ์ •์˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์•„์š”.


    Just โ€” ๊ฐ์ฒด ํ•˜๋‚˜ ๋˜๋Š” ๊ฐ์ฑ„์ง‘ํ•ฉ์„ Observable๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ณ€ํ™˜๋œ Observable์€ ์›๋ณธ ๊ฐ์ฒด๋“ค์„ ๋ฐœํ–‰ํ•œ๋‹ค


    ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด๋ณผ๊ฒŒ์š”. ์•„๋ž˜ ์ฝ”๋“œ ์—ญ์‹œ ๊ณฐํŠ€๊น€๋‹˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์š”.


    @IBAction func exJust1() {
            Observable.just("Hello World")
                .subscribe(onNext: { str in
                    print(str)
                })
                .disposed(by: disposeBag)
        }

    ์œ„์—์„œ just์— "Hello World"๋ผ๋Š” String์„ ๋„ฃ์–ด์คฌ์–ด์š”. just๋Š” ์ด ์•ˆ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋‹ค์Œ ๋ฒˆ์— ๊ทธ๋ƒฅ ์ „๋‹ฌ์ด ๋˜์š”. just ํ›„์— subscribeํ•  ๋•Œ์— ์ „๋‹ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ทธ๋ƒฅ ๊ทธ ๊ฐ’์ด ๋‚ด๋ ค์™€์š”. ์ฆ‰ .subscribe(onNext: { str in ์—์„œ str์€ ์œ„์—์„œ ๋„˜์–ด์˜จ "Hello World"๊ฒ ์ฃ ?


    ๊ทธ๋Ÿผ ์•„๋ž˜์˜ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ๊นŒ์š”?


    @IBAction func exJust2() {
            Observable.just(["Hello", "World"])
                .subscribe(onNext: { arr in
                    print(arr)
                })
                .disposed(by: disposeBag)
        }

    ๋‹น์—ฐํžˆ arr์€ ["Hello", "World"]์—์š”. ์ฆ‰ just๋Š” ๊ทธ ๊ฐ’์ด ๋‹จ์ผ String์ด๋ฉด ๋‹จ์ผ String, Array๋ฉด Array๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•ด์ค˜์š”(๋ญ˜ ๋„ฃ๋“  ๋„ฃ์€ ๊ทธ๋Œ€๋กœ).


  • From

    From์˜ ์ •์˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์•„์š”.

    From โ€” ๋‹ค๋ฅธ ๊ฐ์ฒด๋‚˜ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ Observable๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค


    From์€ ๋จผ์ € ์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ๋ณผ๊ฒŒ์š”.


    @IBAction func exFrom1() {
            Observable.from(["RxSwift", "In", "4", "Hours"])
                .subscribe(onNext: { str in
                    print(str)
                })
                .disposed(by: disposeBag)
        }

    from์— Array๊ฐ€ ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ, ๊ทธ ์•ˆ์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋„์–ด์„œ ๋‚ด๋ ค๋ณด๋‚ด์ค˜์š”. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๋ฌผ์ด


    /** ์ถœ๋ ฅ ๊ฒฐ๊ณผ
    RxSwift
    In
    4
    Hours
    */

    ์ฒ˜๋Ÿผ ๋‚˜์™€์š”. ์ฆ‰ Iterableํ•˜๊ฒŒ str์— ํ•œ๊ฐœ์”ฉ ๋‹ด๊ธฐ๊ฒŒ ๋˜์š”.

    ์—ฌ๊ธฐ์„œ ์‘์šฉ์„ ํ•ด์„œ ๋งŒ์•ฝ 4๊ฐ€ String ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ Int ํ˜•ํƒœ๋ผ๋ฉด?

    Swift๋Š” ํ•œ Array์— ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์–ด์š”. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์š”์†Œ๋“ค๊ณผ ๋‹ค๋ฅด๊ฒŒ 4๋งŒ Int๋ผ๋ฉด


    Observable.from(["RxSwift", "In", 4, "Hours"])
    ...

    ์•„๋ฌด ๋ฌธ์ œ ์—†์ด ์ž‘๋™ํ•ด์š”. ์ฆ‰ from์€ ๊ฐ™์€ ํƒ€์ž…๋งŒ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„์š”. ๋‹ค๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด str์ด๋ผ๋Š” ๋„ค์ด๋ฐ์€ ๋ฐ”๊ฟ”์ฃผ๋Š”๊ฒŒ ์ข‹๊ฒ ์ฃ ? 4๋ผ๋Š” int ๊ฐ’์ด str์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง€๋‹ˆ๊นŒ์š” :)


  • Map


    Map โ€” Observable์ด ๋ฐฐ์ถœํ•œ ํ•ญ๋ชฉ์— ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค


    Map์€ ์‚ฌ์‹ค Swift ๊ณ ์ฐจํ•จ์ˆ˜์™€ ๊ฑฐ์˜ ๊ฐ™์€ ๊ฐœ๋…์œผ๋กœ ๋Š๊ปด์ ธ์„œ ๊ตณ์ด Rx์˜ Operators๋กœ ์„ค๋ช…ํ•˜๊ธด ๊ทธ๋ ‡์ง€๋งŒ, ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•„๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜๋‹ˆ๊นŒ์š”!


    @IBAction func exMap1() {
            Observable.just("Hello")
                .map { str in "\(str) RxSwift" }
                .subscribe(onNext: { str in
                    print(str)
                })
                .disposed(by: disposeBag)
        }

    ์ด์ œ๋Š” ์กฐ๊ธˆ ์ต์ˆ™ํ•ด์ง„ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•ด๋ณด๋ฉด, just๋ฅผ ํ†ตํ•ด "Hello"๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์ด๋ฒˆ์—๋Š” subscribe๊ฐ€ ์•„๋‹Œ map์—์„œ ์ด "Hello"๋ฅผ ๋ฐ›๊ฒ ์ฃ ? ๊ทธ๋ฆฌ๊ณ  mapping์„ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ str์ด "Hello"๋‹ˆ๊นŒ ๊ฒฐ๊ณผ๋Š” "Hello RxSwift" ๊ฐ€ ๋ ๊ฑฐ์—์š”. ๊ทธ ํ›„ subscribeํ•˜๋ฉด str์€ just์—์„œ ๋„˜์–ด์˜จ "Hello"๊ฐ€ ์•„๋‹ˆ๋ผ "Hello RxSwift" ๊ฐ€ ๋˜์š”.

    ํ•œ๋ฒˆ ๋” ๋ณผ๊ฒŒ์š”!


    @IBAction func exMap2() {
            Observable.from(["with", "๊ณฐํŠ€๊น€"])
                .map { $0.count }
                .subscribe(onNext: { str in
                    print(str)
                })
                .disposed(by: disposeBag)
        }

    ์ด ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ from์ด๋‹ˆ๊นŒ element๋ฅผ ํ•œ๊ฐœ์”ฉ ๋„˜๊ฒจ์ฃผ๊ณ , ์ด๋ฒˆ์—๋Š” ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฐ”๋กœ $0์œผ๋กœ ๋ฐ›์•˜๋„ค์š”!

    ๊ทธ๋Ÿผ ๊ฐ ๊ธ€์”จ์˜ ๊ธ€์ž์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒ ์ฃ ?

    mapping์—์„œ ๊ธ€์ž ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  subscribe์—์„œ ๊ทธ๊ฑธ ์ถœ๋ ฅํ•ด์ฃผ๋‹ˆ๊นŒ 4, 3์ด ๋ฐ˜ํ™˜๋ ๊ฑฐ์—์š”.

    count ๋ฉ”์†Œ๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ์ด Int๋ผ ์‚ฌ์‹ค ์—ฌ๊ธฐ์„œ๋Š” subscribe์‹œ์— str์ด ์•„๋‹ˆ๋ผ int๋กœ ๋ช…๋ช…๋˜๋Š”๊ฒŒ ์กฐ๊ธˆ ๋” ์ข‹์€ ๋„ค์ด๋ฐ ๊ฐ™์•„์š”(๋ฌผ๋ก  ์‹ค์ œ ์ƒํ™ฉ์ด๋ฉด ํƒ€์ž… ์ด๋ฆ„๋ณด๋‹ค ๋‹ค๋ฅธ ๊ฑธ ์‚ฌ์šฉํ•˜๊ฒ ์ง€๋งŒ๐Ÿ˜Š).


  • Filter


    Filter โ€” ํ…Œ์ŠคํŠธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ•ญ๋ชฉ๋“ค๋งŒ ๋ฐฐ์ถœํ•œ๋‹ค


    filter๋„ ์œ„์˜ map๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณ ์ฐจํ•จ์ˆ˜์™€ ์ฐจ์ด๊ฐ€ ๋ณ„๋กœ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ์–ด์š”! ๋ง ๊ทธ๋Œ€๋กœ ํ•„ํ„ฐ๋ง์„ ํ†ตํ•ด ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’๋“ค๋งŒ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”๊ฑฐ์ฃ .


    @IBAction func exFilter() {
            Observable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
                .filter { $0 % 2 == 0 }
                .subscribe(onNext: { n in
                    print(n)
                })
                .disposed(by: disposeBag)
        }

    1๋ถ€ํ„ฐ 10๊นŒ์ง€ ๊ฐ’์„ from์—์„œ ๋‚ด๋ ค์ฃผ๋Š”๋ฐ filter์—์„œ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์š”.

    ๋งŒ์•ฝ ๊ทธ ๊ฐ’์ด ์ง์ˆ˜๋ฉด(2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0) subscribe๋กœ ๋‚ด๋ ค๊ฐ€๊ณ  subscribe์—์„œ ์ถœ๋ ฅํ•ด์ค˜์š”.

    ๊ทธ๋Ÿผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์€


    /** ์ถœ๋ ฅ ๊ฒฐ๊ณผ
    2
    4
    6
    8
    10
    */

    ๊ฐ€ ๋˜๊ฒ ๋„ค์š” :]


๐Ÿ“š Operators ์‘์šฉํ•ด๋ณด๊ธฐ


  • ์œ„์—์„œ ํ•ด๋ณธ Operators๋กœ ์‘์šฉ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•ด๋ณผ๊ฒŒ์š”.


    @IBAction func exMap3() {
            Observable.just("800x600")
                .map { $0.replacingOccurrences(of: "x", with: "/") } // 1
                .map { "https://picsum.photos/\($0)/?random" } // 2
                .map { URL(string: $0) } // 3
                .filter { $0 != nil } // 4 
                .map { $0! } // 5
                .map { try Data(contentsOf: $0) } // 6
                .map { UIImage(data: $0) } // 7
                .subscribe(onNext: { image in // 8
                    self.imageView.image = image
                })
                .disposed(by: disposeBag) // 9
        }

    ์šฐ์„  just๋ฅผ ํ†ตํ•ด "800x600"์ด๋ผ๋Š” String์ด ๋‚ด๋ ค๊ฐ€์š”.

    ๊ทธ ๋‹ค์Œ์˜ ํ๋ฆ„์„ ์‚ดํŽด๋ณผ๊ฒŒ์š”(๋ฒˆํ˜ธ๋Š” ์œ„์˜ ์ฃผ์„์„ ๋”ฐ๋ผ์š”).

    1. map์„ ํ†ตํ•ด ๋ฌธ์ž์—ด์˜ x๋ฅผ /๋กœ ๋ฐ”๊ฟ”์ค˜์š”. ๊ทธ๋Ÿผ 800x600์ด 800/600์ด ๋˜๊ฒ ์ฃ ?

    2. map์œผ๋กœ url์„ ๋งŒ๋“ค์–ด์š”. ์œ„์—์„œ ๋‚ด๋ ค์˜จ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚ด๋ ค์˜ค๋‹ˆ๊นŒ

      "https://picsum.photos/800/600/?random"

      ์ด ๋˜๊ฒ ๋„ค์š”! ์ด๊ฑธ ์œ„์—์„œ 1๋ฒˆ์—์„œ replace ํ•ด์ค€๊ฑฐ๊ตฐ์š”.

    3. ์œ„์˜ String์„ URL ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ค˜์š”.

    4. ์—ฌ๊ธฐ์„œ filter๋ฅผ ํ•˜๋Š”๋ฐ ์ƒ๊ฐํ•ด๋ณด๋ฉด 3๋ฒˆ URL์€ Optional์ด์—์š”. ์™œ๋ƒํ•˜๋ฉด ์ด์ƒํ•œ String์„ URL๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ๋‹น์—ฐํžˆ nil์ด ์ƒ๊ฒจ์•ผํ•˜๋‹ˆ๊นŒ์š”! ๊ทธ๋ž˜์„œ nil์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ๊ฑธ๋Ÿฌ์ฃผ๋Š” ์ž‘์—…์„ ํ•ด์š”.

    5. 4๋ฒˆ์—์„œ nil ์ œ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•ด๋„ ์ ˆ๋Œ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ผ์ด ์—†์–ด์š”!

    6. URL์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๋‹ค์šด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์™€์š”.

    7. 6๋ฒˆ์˜ ๋ฐ์ดํ„ฐ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด์š”. ์—ฌ๊ธฐ์„œ ๊ฒฐ๊ณผ๋ฌผ์€ UIImage? ๊ฐ€ ๋˜์š”.

    8. 7๋ฒˆ์˜ UIImage?๋ฅผ ImageView์— ๋„ฃ์–ด์ค˜์š”.

    9. disposeBag์— insert ํ•ด์ฃผ๋Š” ์ž‘์—…์ด์—์š”!

  • ์œ„์˜ ๊ณผ์ •์ด ์ผ๋ จ์˜ ํ๋ฆ„์„ ๋งŒ๋“ค๊ณ  ์žˆ์ฃ ? 1ํŽธ์—์„œ ์ด์•ผ๊ธฐํ•œ RxSwift์˜ ์ •์˜๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ณผ๊ฒŒ์š”

    An API for asynchronous programming with observable streams

    ์—ฌ๊ธฐ์„œ observable streams๋ผ๋Š” ํ‘œํ˜„์ด ๋‚˜์˜ค๋Š”๋ฐ์š”, ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” stream์ด ๋ฐ”๋กœ ์ด๋Ÿฌํ•œ ํ๋ฆ„์„ ์˜๋ฏธํ•ด์š”!

    ์ฆ‰, observe ํ•  ์ˆ˜ ์žˆ๋Š” ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ๊ทธ๊ฒŒ ๋ฐ”๋กœ ์ €ํฌ๊ฐ€ ๋ฐฉ๊ธˆ ํ•ด ๋ณธ ๊ฒƒ์— ๋Œ€ํ•œ ์„ค๋ช…์ด์—์š” :]


3ํŽธ์—์„œ ์ด์–ด๊ฐ€ ๋ณผ๊ฒŒ์š”!


ReactiveX

RxSwift by ๊ณฐํŠ€๊น€

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG more
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ