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

๋ฐ˜์‘ํ˜•

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


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

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

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


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

๐Ÿง‘๐Ÿปโ€๐Ÿ’ป Subscribe ํ•˜๊ธฐ


  • ์šฐ์„  ์•„๋ž˜ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ์€ ๋‹น์—ฐํžˆ Stream์ด ๋ฉ๋‹ˆ๋‹ค.


    Observable.just("HelloWorld")

    ์ด Stream์˜ ์ตœ์ข… ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ subscribe๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์š” :]


    Observable.just("HelloWorld")
        .subscribe()

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด "HelloWorld"๊ฐ€ ๋‚ด๋ ค์˜ค๊ณ  ๊ทธ๋ƒฅ ๋๋‚˜๊ฒŒ๋˜์š”. ์˜ˆ๋ฅผ ๋“ค๋ฉด, Analytic์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๋ณด๋‚ธ ํ›„ ์–ด๋–ป๊ฒŒ ๋˜๋“  ์‹ ๊ฒฝ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š๋‹ค ๋ผ๋Š” ์ƒํ™ฉ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ .

    ํ•˜์ง€๋งŒ subscribe์—๋Š” ์ด๊ฒƒ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์˜ต์…˜์ด ์žˆ์–ด์š”.

    ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ์€ on์ด์—์š”.


    Observable.just("HelloWorld")
        .subscribe(on: (Event<String>) -> Void)

    on์—์„œ ์ด๋ฒคํŠธ ํƒ€์ž…์ด ์™€์š”.

    ์ด๋ฒคํŠธ ํƒ€์ž…์€ enum์œผ๋กœ next, error, completed์˜ 3๊ฐ€์ง€ ํƒ€์ž…์ด ์žˆ์–ด์š”.


    Observable.just("HelloWorld")
        .subscribe { event in
        switch event {
          case .next(let str):
            break
          case .error(let err):
            break
          case .completed:
            break
          }
      }
      .disposed(by: self.disposeBag)

    ๋‹ค๋ฅธ Operator๋“ค์€ return type์ด stream์œผ๋กœ ๋ชจ๋‘ Observable์ด์—์š”. ์œ ์ผํ•˜๊ฒŒ subscribe๋งŒ return ํƒ€์ž…์ด disposable์ด์—์š”. ๊ทธ๋ž˜์„œ subscribe์ด ๋๋‚˜๋ฉด dispose ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•ด์š”.


    next: ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•ด์ค˜์š”.

    error: ์—๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•ด์ค˜์š”.

    completed: ๋ชจ๋“  stream์ด ์™„๋ฃŒ๊ฐ€ ๋˜๋ฉด complete์ด ๋˜์š”.


    ๊ทธ๋ž˜์„œ stream์€ error์ผ ๋•Œ, ๊ทธ๋ฆฌ๊ณ  completed ์ผ ๋•Œ ์ข…๋ฃŒ๊ฐ€ ๋˜์š”.

    just์˜ ๊ฒฝ์šฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— next๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜์ง€๋งŒ, from์˜ ๊ฒฝ์šฐ๋Š” ์–ด๋–จ๊นŒ์š”?


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .subscribe { event in
        switch event {
          case .next(let str):
            break
          case .error(let err):
            break
          case .completed:
            break
          }
      }
      .disposed(by: self.disposeBag)

    ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” 4๋ฒˆ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถˆ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— next ์—ญ์‹œ 4๋ฒˆ ๋ถˆ๋ฆฌ๊ฒŒ ๋  ๊ฑฐ์—์š”!

    ๊ทธ๋Ÿผ ์•„๋ž˜์˜ ๊ฒฝ์šฐ ์–ด๋–ค ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์˜ฌ์ง€ ์˜ˆ์ƒํ•ด๋ณผ๊ฒŒ์š”.


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .subscribe { event in
        switch event {
          case .next(let str):
                    print("next: \(str)")
              break
          case .error(let err):
              print("next: \(err.localizedDescription)")
            break
          case .completed:
              print("completed")
            break
          }
      }
      .disposed(by: self.disposeBag)

    ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์€ ์•„๋ž˜์™€ ๊ฐ™์•„์š”


    /**
    next: test
    next: arrray
    next: 1
    next: ์ž…๋‹ˆ๋‹ค.
    completed
    */

    ์—ฌ๊ธฐ์„œ ํฌ์ธํŠธ๋Š” completed๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์ด์—์š”. event์—์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ next๋กœ ๋„˜์–ด์™”๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ stream์ด ์ข…๋ฃŒ๊ฐ€๋˜๋ฉด ๋ฌด์กฐ๊ฑด .completed์˜ case๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์ด์ฃ .

    ์ด๋ฒˆ์—” ์—๋Ÿฌ๋ฅผ ์ผ์œผ์ผœ ๋ณผ๊ฒŒ์š”.


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe { event in
        switch event {
          case .next(let str):
                    print("next: \(str)")
              break
          case .error(let err):
              print("next: \(err.localizedDescription)")
            break
          case .completed:
              print("completed")
            break
          }
      }
      .disposed(by: self.disposeBag)

    single์˜ ๊ฒฝ์šฐ 1๊ฐœ์˜ ๊ฐ’๋งŒ ๋“ค์–ด์˜ค๊ณ  complete bar๊ฐ€ ์žˆ์—ˆ์ฃ ? single์ธ๋ฐ ์—ฌ๋Ÿฌ ๊ฐ’์ด ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด์š”. ๊ทธ๋ž˜์„œ ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋  ๊ฑฐ์—์š”.


    /**
    next: test
    error: The operation ๋ธ”๋ผ๋ธ”๋ผ (์—๋Ÿฌ ๋ฉ”์„ธ์ง€)
    */

    ์—ฌ๊ธฐ์„œ๋„ ์ค‘์š”ํ•œ ๊ฑด stream์ด๊ธฐ ๋•Œ๋ฌธ์— error๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „ ์ฒซ๋ฒˆ์งธ stream์€ ๋™์ž‘์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๋‹จ์ˆœํžˆ ์—๋Ÿฌ๋งŒ ๋กœ๊น…๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ Rx์—์„œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋Š” ์‹œ์  ์ด์ „๊นŒ์ง€ ์ž‘์—…์€ ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”!

    ๊ทธ๋Ÿผ ์œ„์˜ ์ƒํ™ฉ ๋•Œ๋ฌธ์— ์ƒ๊ธฐ๊ธฐ ์‰ฌ์šด ๋ฌธ์ œ๋Š” ์–ด๋–ค ๊ฒฝ์šฐ์ผ๊นŒ์š”?


    // Progress on
    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe { event in
        switch event {
          case .next(let str):
                    print("next: \(str)")
              break
          case .error(let err):
              print("next: \(err.localizedDescription)")
            break
          case .completed:
              // Progress off
              print("completed")
            break
          }
      }
      .disposed(by: self.disposeBag)

    ์œ„์˜ ๊ฒฝ์šฐ error๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด Progress๊ฐ€ ์ ˆ๋Œ€ off ๋˜์ง€ ์•Š์•„์š”. stream์ด ๊ณ„์† next๋งŒ ์ง„ํ–‰๋  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— stream์˜ ์ข…๋ฃŒ๊ฐ€ ์žˆ์–ด completed๊ฐ€ ๋™์ž‘ํ•˜์ง€๋งŒ error๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ทธ ์ฆ‰์‹œ stream์ด ๋ฉˆ์ถ”๊ธฐ ๋•Œ๋ฌธ์— complete bar ํƒ€์ž„๊นŒ์ง€ ๋Œ์•„์˜ค์ง€ ์•Š์•„ .completed๊ฐ€ ๋™์ž‘ํ•  ์ผ์ด ์—†๊ฒŒ ๋˜์š”.


  • ์ด๋ฒˆ์—” subscribe onNext๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”!

    onNext๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„๋˜์š” :]


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe(onNext: ~~, onError: ~~, onCompleted: ~~, onDisposed: ~~)

    onNext๊ฐ€ ์•„๋‹Œ ๋‹จ์ˆœ on์œผ๋กœ ๊ตฌํ˜„์ด ๋˜์—ˆ์„ ๋•Œ์—๋Š” switch์— ๋น ์ง€๋Š” ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์•ˆ๋ฌ์–ด์š”. case๊ฐ€ 3๊ฐœ์ด๋‹ˆ๊นŒ ๋ฌด์กฐ๊ฑด switch์— 3๊ฐ€์ง€ ๊ฒฝ์šฐ ๋ชจ๋‘๊ฐ€ ๊ตฌํ˜„ ๋˜์–ด์•ผํ•˜์ฃ .

    ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒŒ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ๊ท€์ฐฎ์„ ๋•Œ๊ฐ€ ์žˆ์ฃ ? ๊ทธ๋ž˜์„œ Next์ผ ๋•Œ๋งŒ ์ž‘์—…ํ• ๊ฑฐ์•ผ ๋ผ๋Š” ์ƒํ™ฉ์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ์–ด์š”. ๋‚˜๋จธ์ง€๋Š” ํŠนํžˆ optional์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ์ž‘์„ฑํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์ฃ ! ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด์š”.


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe(onNext: { event in
                              print(event)
                         }
                ).disposed(by: self.disposeBag)

    ์ด์ œ onError๋‚˜ onCompleted, onDisposed๋„ ์ž‘์„ฑํ•ด๋ณผ๊ฒŒ์š”!


    Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe(onNext: { event in
                              print(event)
                         },
                 onError: { err in 
                              print(err.localizedDescription)
                          },
                 onCompleted: {
                                               print("onCompleted")
                                             },
                 onDisposed: {
                                               print("disposed")
                                             }
                ).disposed(by: self.disposeBag)

    ์—ฌ๊ธฐ์„œ ์—ญ์‹œ error๊ฐ€ ๋ฐœ์ƒํ•  ํ…๋ฐ์š”, on์—์„œ ํ–ˆ๋˜ ์˜ˆ์‹œ์™€ ๋‹ค๋ฅธ ๊ฒƒ์€ "disposed"๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค๋Š” ์ ์ด์—์š”. onDisposed๋Š” ๋ง ๊ทธ๋Œ€๋กœ dispose๋  ๋•Œ์ด๊ณ , dispose๋Š” Error๋‚˜ Complete ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”!


    ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์ฝ”๋“œ๋Š” ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•ด์š”. ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ข€ ๋” ๊ฐ€๋…์„ฑ ์ข‹๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์š”.


    private func output(_ event: Any) {
      print(event)
    }
    
    func exampleFrom() {
      Observable.from(["test", "array", 1, "์ž…๋‹ˆ๋‹ค."])
        .single()
        .subscribe(onNext: output)
          .disposed(by: self.disposeBag)
    }

    ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ณ  ๊น”๋”ํ•ด์กŒ์–ด์š”. onNext ๋’ค์— ๋“ค์–ด๊ฐˆ ํด๋กœ์ €๋Š” input parameter๊ฐ€ event, ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ์ด Void๋กœ print์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์กฐ๊ฑด์— ๋งž์ถ˜ ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌ ๊ฐ€๋Šฅํ•ด์š”.


4ํŽธ์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค! 5ํŽธ์—์„œ๋Š” observeOn์ฒ˜๋Ÿผ Main Thread๊ฐ€ ์•„๋‹Œ Concurrency Thread์—์„œ ์ž‘์—…ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐ ํ•ด๋ณผ๊ฒŒ์š”! :]

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
๊ธ€ ๋ณด๊ด€ํ•จ