[RxSwift๐Ÿฆˆ] #5 Variable & BehaviorRelay

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

iOS

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

1. ๋งŒ๋“ค๊ณ  ์‹ถ์—ˆ๋˜ ๊ฒƒ : ์ฒดํฌ๋ฐ•์Šค

@IBAction checkButtonTouched(sender: UIButton) {
    sender.isSelected = !sender.isSelected
    activateButton(isSelected: sender.isSelected)
}

์ฒ˜์Œ๋ถ€ํ„ฐ RxSwift ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ฌด๋ฆฌ์ผ ๊ฒƒ ๊ฐ™์•„์„œ View๋ฅผ ๋‹ค ์งœ๊ณ  ๋™์ž‘์„ ์ œ๋Œ€๋กœ ํ•˜๋Š”์ง€ ํ™•์ธ ํ›„, ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ธธ๋ž˜ RxSwift๋กœ ๋ฆฌํŒฉํ† ๋ง์„ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

RxCocoa ์— checkButton.rx.tap ์ด๋ผ๋Š” ์ข‹์€ method ๊ฐ€ ์žˆ์—ˆ์ง€๋งŒ sender.isSelected ๋ฅผ ํ‘œํ˜„ํ•  ๋ฐฉ๋ฒ•์ด ์—†์—ˆ๋‹ค.



2. Variable

๊ทธ๋ž˜์„œ ์ฐพ๋˜ ์ค‘ Variable ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด๊ฐ€ ์›ํ•˜๋˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ์†Œ์Šค๋ฅผ ์ฐพ๊ฒŒ ๋˜์—ˆ๊ณ , ๋Œ€์ถฉ ์ดํ•ด๋Š” ๊ฐ€์ง€๋งŒ ๋ญ”๋ง์ด์•ผ.. ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

let checkBoxValid = Variable(true)

checkBoxValid.asObservable()
    .bindTo(checkBoxButton.rx.isSelected)
    .disposed(by: disposeBag)

checkBoxButton.rx.tap
    .subscribe(onNext: {
    checkBoxValid.value = !checkBoxValid.value
    })
    .disposed(by: disposeBag)

์ถœ์ฒ˜ : https://github.com/ReactiveX/RxSwift/issues/1172


์—ฌ๊ธฐ์—์„œ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ,

Varaiable์€ BehaviorSubject ์˜ ํ˜„์žฌ ๊ฐ’์„ State ๋กœ ๊ฐ€์ง€๋Š” ๋…€์„์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ ๊ฐ’์„ Observable ์ฒ˜๋Ÿผ ๋ณด์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค๊ณ  ์„ค๋ช…ํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ

  • Variable(true) ์ด๋ ‡๊ฒŒ ๊ฐ’์„ Variable ๋กœ ๊ฐ์‹ธ์„œ ํ‘œํ˜„ํ•˜๊ณ 
  • ํ•ด๋‹น ๊ฐ’์„ subscribe ํ•˜๊ณ  ์‹ถ์œผ๋ฉด .asObservable() ์„ ํ†ตํ•ด Observable ๋กœ ๊ฐ„์ฃผํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ๊ฐ’์„ ๊บผ๋‚ด ์“ฐ๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋„ฃ๊ณ  ์‹ถ์œผ๋ฉด .onNext() ๋Œ€์‹  checkBoxValie.value ์ฒ˜๋Ÿผ .value๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    • ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ์ด ๋ณด์žฅ๋˜๊ณ , ํ• ๋‹น ํ•ด์ œ ์‹œ ์ž๋™ ์™„๋ฃŒ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— .onError, .onCompleted ์—ญ์‹œ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

์‚ฌ์šฉ์€ ์•„๋ž˜ ๋ชจ๋“  ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Observable ์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜๋งŒ ์žˆ๋Š” ์ƒํ™ฉ
  • Subject ์ฒ˜๋Ÿผ ๊ฐ’์„ ์ฃผ๊ณ  ๋ฐ›๋Š” ์ƒํ™ฉ
  • ๊ตฌ๋… ์—†์ด ๊ทธ๋ƒฅ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์žˆ์–ด๋„ ๋˜๋Š” ์ƒํ™ฉ (์œ„์—์„œ ๋งํ•œ ์ผ๋ฐ˜ ๊ฐ’์„ Observable ์ฒ˜๋Ÿผ ์‚ฌ์šฉ)



3. BehaviorRelay

๊ทธ๋Ÿฐ๋ฐ, Variable ์€ deprecated ์ด๋‹ˆ BehaviorRelay ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๋Š” ๊ฒฝ๊ณ  ๋ฌธ๊ตฌ๊ฐ€ ๋– ์„œ BehaviorRelay ๋กœ ์ฝ”๋“œ๋ฅผ ๊ณ ์ณ๋ณด์•˜๋‹ค.

// checkButton : ์œ„์˜ ๊นƒํ—™ ์ฝ”๋“œ๋ฅผ BehaviorRelay๋กœ ๋ฐ”๊ฟˆ.
// checkButtonIsSelected ๋ผ๋Š” observable ๋งŒ๋“ค์–ด์„œ,
// checkButton.isSelected ํ•ด๋‹น relay๋ฅผ ๊ตฌ๋…ํ•˜๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค.
// ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด checkButtonIsSelected ์— ์ •๋ณด ๋ณด๋‚ด์„œ ๋ฐ”๋€Œ๊ฒŒ.
let checkButtonIsSelected = BehaviorRelay(value: false)
checkButtonIsSelected.asObservable()
  .bind(to: checkButton.rx.isSelected)
  .disposed(by: disposeBag)

_ = checkButton.rx.tap
  .subscribe(onNext:  {
    checkButtonIsSelected.accept(!checkButtonIsSelected.value)
    self.activateButton(isSelected: checkButtonIsSelected.value)
  })
  .disposed(by: disposeBag)

Variable ๋Œ€์‹  BehaviorRelay ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

๋Š”, Varaible ์ด ์˜๋„์น˜ ์•Š๊ฒŒ ๊ฐ’์„ ๋ฐ”๊ฟ€ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ณ , ์ด๋ฆ„ ์—ญ์‹œ๋„ RxSwift ์˜ ์ปจ๋ฒค์…˜๊ณผ ๋งž์ง€ ์•Š๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๋ฉด์—์„œ๋„ ํšจ์œจ์ ์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค. Variable ์ด ์ง๊ด€์ ์ธ ์ด๋ฆ„์ด๋ผ ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด๋‹ˆ Relay ๋ผ๋Š” ๊ฐœ๋…์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต์ง€๋Š” ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.




+

์ธํ„ด์„ ํ•˜๋ฉด์„œ, ํšŒ์‚ฌ์˜ ํ”„๋กœ์ ํŠธ๊ฐ€

  1. customView : xib + SnapKit
  2. RxSwift
  3. viper design pattern

๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. 2, 3๋ฒˆ์€ ๋ฉด์ ‘๋•Œ๋„ ๋“ฃ๊ธด ํ–ˆ์—ˆ์ง€๋งŒ ์ „๋ถ€ ๊ณต๋ถ€ํ•˜๊ธฐ์—” ๋ฌด๋ฆฌ๊ฐ€ ์žˆ์–ด์„œ RxSwift 4์‹œ๊ฐ„ ๊ฐ•์˜๋งŒ ๋จผ์ € ๋“ค์—ˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ... ์ถœ๊ทผํ•˜๋ฉด์„œ 1์ฃผ์ผ ๊ฐ„ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๊นŒ๋ณด๊ณ  ๊ฐ„๋‹จํ•œ ํ™”๋ฉด ๊ตฌํ˜„์„ ํ•ด๋ณด๋‹ˆ rx ๊ฐ€ ๊ณฐํŠ€๊น€๋‹˜ ๊ฐ•์˜์ฒ˜๋Ÿผ ์‰ฝ์ง€๋งŒ์€ ์•Š์•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ๋™์•ˆ ์ฝ”๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ทฐ ์žก๋‹ค๊ฐ€ xib๋ฅผ ์“ฐ๋‹ˆ ์žฅ๋‹จ์ ์ด ๋„ˆ๋ฌด ๋ช…ํ™•ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์—ฌ๋Ÿฌ ์†Œ์Šค๋ฅผ ์ฐพ์œผ๋ฉด์„œ ๊ณต๋ถ€ํ•˜๋˜ ์ค‘

RxSwift๋Š” ๋ณด์˜๋‹˜์˜ ์ •๋ฆฌ ์†Œ์Šค๊ฐ€ ๋”ฐ๋ผ๊ฐ€๊ธฐ ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์„œ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์žˆ๋‹ค. GITGET ์—„์ฒญ ์ž˜ ์“ฐ๊ณ  ์žˆ๋Š”๋ฐ ์ด๋ถ„์ด ๋งŒ๋“ ๊ฑฐ์˜€๋‹ค..!!!

ํ‡ด๊ทผํ•˜๋ฉด ๋ป—์–ด์„œ ๋ธ”๋กœ๊ทธ ์“ธ ์‹œ๊ฐ„์ด ๋ณ„๋กœ ์•ˆ๋‚˜์ง€๋งŒ... ํŒŒ์ดํŒ…!!

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

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