[RxSwift๐Ÿฆˆ] #2 ์“ฐ๋ ˆ๋“œ ํ™œ์šฉ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ

๋˜์ƒยท2022๋…„ 1์›” 5์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
1/42
post-thumbnail

3~5์˜ ๊ฒฝ์šฐ๋Š” #1 ์ฝ”๋“œ์—์„œ ๊ฐœ๋…์„ ๋๋‚ด๋ฒ„๋ ธ๊ธฐ ๋•Œ๋ฌธ์—, https://github.com/iamchiwon/RxSwift_In_4_Hours/blob/master/README.md ์— ์žˆ๋Š” ๋ชฉ์ฐจ๋งŒ ๋‚จ๊ฒจ๋‘”๋‹ค.

3. Observable Life-Cycle

  • Subscribed
  • Next
  • Completed / Error
  • Disposabled

4. ์ˆœํ™˜์ฐธ์กฐ์™€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ

-> dispose๋กœ ์—†์• ์ฃผ์ž.

๋ฐฉ๋ฒ•1. disposable ๋ณ€์ˆ˜ ์‚ฌ์šฉ

  • var disposable: Disposable? ์œผ๋กœ ์“ฐ๊ณ  ์—ฌ๊ธฐ์— disposable ์„ ์ €์žฅํ–ˆ๋‹ค๊ฐ€, disposable.dispose()

๋ฐฉ๋ฒ•2. dispose ํ•  ๊ฒƒ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ๋Š”, var disposables: [Disposable] = [] ๋กœ ๋„ฃ์—ˆ๋‹ค๊ฐ€ ๋ฐฐ์—ด ์ „์ฒด ์›์†Œ์— ๊ฐ๊ฐ dispose()

๋ฐฉ๋ฒ•3. disposeBag ์ด์šฉ

var disposeBag = DisposeBag()

@IBAction func onLoad() {
    editView.text = ""
    setVisibleWithAnimation(self.activityIndicator, true)
    
    // let d = 
    downloadJson(MEMBER_LIST_URL)
        .subscribe(onNext: { json in
            DispatchQueue.main.async {
                self.editView.text = json
                self.setVisibleWithAnimation(self.activityIndicator, false)
            }
        })
		.disposed(by: disposeBag)
        // disposables.append(d)  // 2
        // ์ด๊ฒƒ๋งˆ์ € ๊ท€์ฐฎ๋‹ค? 3. disposed ๋ฅผ ์‚ฌ์šฉ. disposabled์„ ๋ณ€์ˆ˜๋กœ ๋ฐ›์ง€ ์•Š์•„๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
        
        // d.dispose() // 1. ์ทจ์†Œํ•˜๊ณ  ์‹ถ์€ ๋™์ž‘์ด ์žˆ์„ ๋•Œ disposable์„ ํ˜ธ์ถœํ•ด์„œ ์ทจ์†Œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
}



override func viewWill Disappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // disposables.forEach($0.dispose()) // 2
} 

5. ์“ฐ๋ ˆ๋“œ ๋ถ„๊ธฐ

  • DispatchQueue, OperationQueue
  • observeOn, subscribeOn

6. Stream์˜ ๋ถ„๋ฆฌ ๋ฐ ๋ณ‘ํ•ฉ

  • share
  • merge : ์—ฌ๋Ÿฌ๊ฐœ์˜ observable์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ค€๋‹ค. ์•ž์˜ observable๊ณผ type์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • zip : ์œ„์˜ observable๊ณผ ์•„๋ž˜ observable์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ ์Œ์„ ๋งŒ๋“ค์–ด์„œ ๋‚ด๋ ค์ค€๋‹ค. ๊ทผ๋ฐ, ์ง์ด ์•ˆ๋งž์•„์„œ ๋‚จ๋Š” ์• ๊ฐ€ ์žˆ์œผ๋ฉด ๋‚ด๋ ค์˜ค์ง€ ์•Š๋Š”๋‹ค.
  • combine
    - combineLatest : zip ์€ ์Œ์„ ๋งŒ๋“ค๊ฒŒ ์—†์œผ๋ฉด ๋ชป ๋‚ด๋ ค๊ฐ€๋Š” ๋ฐ˜๋ฉด, combineLatest๋Š” ์•„๋ž˜ observable์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฒƒ๊ณผ ์Œ์„ ์ง€์–ด์„œ ๋‚ด๋ ค์˜จ๋‹ค.




์ถœ์ฒ˜

์œ ํŠœ๋ธŒ ๊ณฐํŠ€๊น€๋‹˜ RxSwift 4์‹œ๊ฐ„์— ๋๋‚ด๊ธฐ
https://youtu.be/iHKBNYMWd5I

RxSwift 4์‹œ๊ฐ„์— ๋๋‚ด๊ธฐ github
https://github.com/iamchiwon/RxSwift_In_4_Hours

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€