0

I have one filter type enum

import RxSwift import RxCocoa

enum FilterType {
    case all
    case rental
    case purchased
}

var currentCategory: Driver<FilterType> = Driver.just(.all)

whenever I am updating the currentCategory on segment click. like below currentCategory = Driver.just(.purchased).

I am always getting the same value all every time. I am new to RXSwift and RXCocoa. Please help me to get out of this situation. Thanks in advance.

1 Answers1

1

You are using RxSwift in incorrect way: every time when you assign to currentCategory, your subscriptions gets disposed. You need to use different approach:

let currentCategory = BehaviorRelay<FilterType>(value: .all)

and then in your code set new value for BehaviorRelay:

currentCategory.accept(.purchased)

or bind it to your UI control:

segmentControl.rx.value
  .map {
    switch $0 {
    case 0: return FilterType.all
    case 1: return FilterType.rental
    case 2: return FilterType.purchased
    default: return FilterType.all
    }
  }
  .bind(to: viewModel.currentCategory)
  .disposed(by: disposeBag)
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
  • Thanks. I will check it out. – Tapash Mollick Nov 26 '19 at 17:22
  • I have one more doubts. I have to refresh the list based on the segment control value changed I have taken one PublishSubject like below let selectedPurchaseTabSegmentPublisher = PublishSubject< FilterType >() I don't know how to achieve this using publish subject. Could anyone please help me? – Tapash Mollick Dec 06 '19 at 07:34